I am a noobie at React and I am trying to make a Bootstrap dropdown. The html that I am attaching to is here:
<ul class="dropdown-menu" id="dropdown">
</ul>
And here is what I want to put in my render method to insert inside of my html:
render: function() {
return (
<li>Books</li>
<li>Podcasts</li>
<li>Tech I Like</li>
<li>About me</li>
<li>Add a Blog</li>
);
}
But of course I can only return one element. What is the right way of doing this in React? How could I add multiple <li>'s into a dropdown like this? I tried wrapping the whole thing in a <div>, but that messes up my css.
react bootstrap makes working with react & bootstrap a bit easier:
render: function(){
return (
<DropdownButton title="Dropdown">
<MenuItem href="#books">Books</MenuItem>
<MenuItem href="#podcasts">Podcasts</MenuItem>
<MenuItem href="#">Tech I Like</MenuItem>
<MenuItem href="#">About me</MenuItem>
<MenuItem href="#addBlog">Add a Blog</MenuItem>
</DropdownButton>
);
}
This looks about the same, but has event-handlers & adds all the right classes. As #sophie-alpert said, though, render must return a single DOM parent element.
Unfortunately this is one situation where React's ability to return only a single node from render is annoying. Your best bet is probably to return the <ul> itself from render:
render: function() {
return (
<ul className="dropdown-menu" id="dropdown">
<li>Books</li>
<li>Podcasts</li>
<li>Tech I Like</li>
<li>About me</li>
<li>Add a Blog</li>
</ul>
);
}
then render that entire component into another container like a <div>. In a future version of React we're hoping to remove this restriction so that something like your original code will work.
You can use react-select react component.It is very simple and easy to use.
var Select = require('react-select');
var options = [
{ value: 'one', label: 'One' },
{ value: 'two', label: 'Two' }
];
function logChange(val) {
console.log("Selected: " + val);
}
<Select
name="form-field-name"
value="one"
options={options}
onChange={logChange}
/>
If you don't want to employ rebuilt dependencies like react bootstrap to work with both react and bootstrap, simply do the important js tricks which they do. In your case, basically a dropdown click event toggles the popper wrapper with .show css class. So you can define an onClick method and toggle the class inside it. For example in the following code I get the nextSibling and then toggle the class name for it with .show and finally inner div will be shown:
<div className="dropdown">
<a className="btn text-light" href="#" onClick={(e) => this.handleOption(e)}>open dropdown</a>
<div className="dropdown-menu dropdown-menu-left dropdown-menu-arrow">
<a className="dropdown-item" href="#">edit</a>
<a className="dropdown-item" href="#">delete</a>
</div>
</div>
Note that this tricks help you in case of few bootstrap functionalities in your project, otherwise employ rebuilt dependencies to have a clean and standard code.
Related
I'm trying to "converting" my code from HTML to ReactJS. What I tried and I couldn't make it worked is this line of code:
<div className = "title-bar" data-responsive-toggle = "center_menu">
<button className = "menu-icon" type = "button" data-toggle></button>
<div className = "title-bar-title"></div>
</div>
When I'm pressing that hamburger icon, that list "Home, Extensii etc" should appear. In HTML I made it worked fine. ReactJS it's not taking my data-responsive-toggle center_menu id. How should look the code, what is the correct syntax?
P.S.: I'm new in React, I'm trying to learn it.
well I don't know about whether you want it to look normal all the way and just have that burger on smaller screens or it's a burger menu all along but generally speaking for what you have asked, you need conditional classes to actually toggle between display:block; and display:none; and you need to handle that with the click event, so basically:
<div className ="title-bar">
<button className = "menu-icon" type = "button" onClick={this.toggleMenu}></button>
<div className = "title-bar-title"></div>
</div>
<div className={`example-menu ${this.isclicked}?" collapsed-menu": " closed-menu" `}>
<ul class = "menu dropdown" data-dropdown-menu>
<li>Home
....
<li>Contact</li>
</ul>
</div>
and that's what this.toggleMenu does:
toggleMenu() {
this.setState(state => ({
isclicked: !state.isclicked
}));
}
needless to talk about those two classes collapsed-menu and closed-menu, they would be sth like these:
.collapsed-menu{
....
display:block;
}
.closed-menu{
...
display:none;
}
There are several tutorials available on how to do it, e.g. here: https://css-tricks.com/hamburger-menu-with-a-side-of-react-hooks-and-styled-components/
Also you could use npm libraries to achieve the expected results.
what about the css (media query), did you specify the min-width value for the display??
I am trying to refactor from class based to functional. While doing so I will need to use hooks instead of setting this.state etc.. I am trying to get a FORM to open when i click a button. The button will also change from "add reply" to "submit comment" once the form opens. I am stumped. This is the best thing I could come up with... Doesnt work. in fact, it makes my "add reply" button completely disappear. Any thoughts on this? Here is the code that I have written. inside of the comment I am trying to return a component using ternary....
image of component as-is
import React, {useState} from 'react';
import FormOpen from './FormOpen';
const CommentCreated = (props) => {
const [resource, setResource] = useState([{visible: false, addReply: 'Add Reply'}]);
return (
<div className="ui threaded comments">
<h3 className="ui dividing header">Comments</h3>
<div className="comment">
<a href="/" className="avatar">
<img alt="avatar" src= {props.avatar} />
</a>
<div className="content">
<a href="/" className="author">{props.author}
</a>
<div className="metadata">
<span className="date">Today at 5:42PM</span>
</div>
<div className="text">{props.content}
</div>
<div className="actions">
{resource.visible ? <FormOpen /> : null}
<a className="reply" onClick={() => {setResource([{visible: true, addReply: 'Submit Comment'}]);}}>{resource.addReply}
</a>
<a className="save">Save</a>
<a className="hide">Hide</a>
</div>
</div>
</div>
</div>
);
};
export default CommentCreated;
You should replace the setResource(['Submit Comment', true]) with :
setResource([{
visible: true,
addReply: 'Submit Comment'
}])
To match the shape of your state.
EDIT:
Here is a working example based on your code.
As I can see from your example, your resource state doesn't need to be an array but simply an object.
Furthermore, as you are using hyperlinks <a />, you must use preventDefault() on your event being triggered if you don't want the page to refresh.
I created a simple navigation menu with Bootstrap. It's working fine in pure javascript, but when adapting it into react, the hamburger icon doesn't function (nothing happens on click). I installed bootstrap with
npm install --save bootstrap
And then added the bootstrap css to index.html in the public folder:
link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.1/css/bootstrap.min.css" integrity="sha384-WskhaSGFgHYWDcbwN70/dfYBj47jz9qbsMId/iRN3ewGhXQFZCSftd1LZCfmhktB"
My jsx is as follows:
<nav className="navbar navbar-expand-sm navbar-dark bg-dark">
<div className="container">
<button className="navbar-toggler" data-toggle="collapse" data-target="#navbarNav"><span className="navbar-toggler-icon"></span></button>
<div className="collapse navbar-collapse" id="navbarNav">
<ul className="navbar-nav ml-auto">
<li className="nav-item">
<Link to="/app/portfolio" className="nav-link">PORTFOLIO</Link>
</li>
<li className="nav-item">
<Link to="/app/about" className="nav-link">ABOUT</Link>
</li>
<li className="nav-item">
<Link to="#create-head-section" className="nav-link" style={{fontStyle: 'italic'}}>Personal art</Link>
</li>
<li className="nav-item">
<Link to="#share-head-section" className="nav-link">CONTACT</Link>
</li>
</ul>
</div>
</div>
</nav>
Again, everything looks fine except that the hamburger icon is not functioning.
The bootstrap show class is used to display the collapsible menu items. So, the task is to simply add the show class conditionally on click of the hamburger icon.
Just follow these simple steps to achieve this functionality.
Add a showCollapsedMenu(the name is up to you) property with initial value of false in your state like this:
state={
showCollapsedMenu: false
}
Then declare a function like this which when called will reverse the current state:
toggleMenu = () => {
this.setState({
showCollapsedMenu: !this.state.showCollapsedMenu
})
}
The above function will be called whenever the hamburger icon is clicked. So implement the onCLick method on the hamburger icon like this:
<button
className="navbar-toggler"
type="button"
onClick={this.toggleMenu} // <=
data-toggle="collapse"
data-target="#navbarNav">
<span className="navbar-toggler-icon"></span>
</button>
Now create a const show which will conditionally add the show class depending on the state of showCollapsedMenu:
const show = (this.state.showCollapsedMenu) ? "show" : "" ;
Now finally add this show to the div with collapse navbar class like this:
<div className={"collapse navbar-collapse " + show} id="navbarNav">
Note: Mixing jQuery with React is not recommended as they both manipulate the DOM differently. While it may seem an easy solution, it might result in bigger problems.
Bootstrap events require jQuery, Popper and Bootstrap.js source. That page will also let you know which components require JS. You can include jQuery, Popper and bootstrap.js in the index.html file, where you load your bundle. Either add that, or simply check out Reactstrap, which implements Bootstrap components in React.
According to https://www.npmjs.com/package/bootstrap#whats-included, the npm version of Bootstrap doesn't include jQuery, but the navbar component needs it, so you might need to add https://www.npmjs.com/package/jquery to your dependencies.
I am new at react js. Currently getting trouble while trying to use a local image as default icon for a button. But getting no luck.
var NavigationBar = React.createClass({
render: function(){
return (
<aside style={{float:"left",background:"#9999FF", height:"200px"}} class="navclass">
<ul>
<li>Templates
<ul>
//<form onSubmit={this.handleSubmit}>
<button><Image source={require('./template1.png')} style={{width:"28px", height:"28px"}}></Image>Template1</button>
<button><Image source={require('./template2.png')} style={{width:"28px", height:"28px"}}></Image>Template2</button>
//</form>
</ul>
</li>
<li>Figures</li>
<li>Text</li>
<li>Background Color</li>
<ImageUpload/>
</ul>
</aside>
);
}
});
Sorry, what are you talking about, React or React-Native ?
<Image /> is definetly a react-native component, and as I see from syntax you have copied this piece of code from some react-native example or tutorial, and all other tags are html... Maybe you should better try to use simple <img src='...' />. It should work.
My isotope filtering is working perfectly when the identifying class is hard-coded in the grid elements, but it fails to function when the elements are rendered using jQuery Ajax call in ReactJS, and the identifying class are set using props passed to it by parent component.
When I hard-code the className="block hvr-reveal CategoryA " , then it seems to work fine, but when I pass the same through props, it doesn't.
Also it's getting displayed if I append a <p> tag right after the <img> tag and display {this.props.category}.
var Template = React.createClass({
render: function() {
return (
<div className="container text-center">
<div className="block hvr-reveal {this.props.category} " data-category={this.props.category} >
<img src={this.props.url}></img>
</div>
</div>
);
}
});