getting the state of navbar in bootstrap 4 using React js - reactjs

I'm trying to toggle some styling depending on whether the navbar is opened or closed (in mobile and tablet views), I have seen solutions for jQuery but i want to do it in react js.
i have tried setting a state on the hamburger button and toggling it on user interaction but it's really not a good solution and it's flawed since sometimes the user can close the navbar without clicking the hamburger button, instead i want to get the actual state of the navbar
is this possible in react js? if so how?

Use a boolean state variable and toggle it when the Hamburger is clicked...
...
constructor(props) {
super(props);
this.state = { showNav: true };
this.toggleNav = this.toggleNav.bind(this);
}
toggleNav() {
this.setState({
showNav: !this.state.showNav
})
}
In the Navbar markup...
<nav>
<button className="navbar-toggler" type="button" onClick={this.toggleNav}>
<span className="navbar-toggler-icon"></span>
</button>
<div className={(showNav ? 'show' : '') + ' collapse navbar-collapse'}>
</div>
</nav>
Demo: https://codeply.com/p/z0rSHluhPi

Related

All buttons close on click of one button

please i am building a FAQs page and i made a series of buttons which when clicked displays a hidden paragraph underneath each button, now the issue is all buttons respond to one button being clicked on and they all display their respective paragraphs, i want each button to display it's own hidden paragraph alone.
this is the react code i used
class FAQ extends React.Component {
constructor () {
super()
this.state = {
isHidden: true,
}
}
toggleHidden () {
this.setState({
isHidden: !this.state.isHidden})
}
<div className="faq--button">
<button onClick={this.toggleHidden.bind(this)}>button to click</button>
{!this.state.isHidden && <p>lorem ipsum"</p>}
</div>
Currently isHidden is used for all buttons, which will have the same effect in each button. So you need to add separate states for each button. Like isHidden2, isHidden3....

Btn classes is not clickable in react.js?

Here I am creating a button in which when I click on button, it redirects to another page. Now the problem arises when I am setting className="btn". If I didn't use btn in className then this div is clickable but when I am setting className="btn" then This button is not clickable.
<div className="btn col">
<Link to={`/category/${category.slug}`}>{category.name}</Link>
</div>
Firstly, If you can show on your post your css code, It will be helpful to understand what is happening and to let people test it.
First Solution (useHistory)
I do not know if you have experience using React Router (https://reactrouter.com/web/api/Hooks) but what you can do is to add an onClick event to the div element and call history.push(). I let you an example below
import { useHistory } from "react-router-dom";
function Home() {
let history = useHistory();
function handleClick() {
history.push("/page");
}
return (
<div onClick={handleClick}>
Go to a page
</div>
);
}
Second Solution
I am not sure about what is happening with your css code but I am sure about is a z-index problem. Probably div is over your link component and you cannot click on it. I've found this post (Links not working inside div) check if implementing the solution, it solvers your problem

I have to show a pop on click of a span but elements not appearing in the DOM

I am trying to display a div pop up on click of a span. Even though the code inside the function is executing, the div is not at all visible in the DOM.
Am not sure where I am going wrong.
<span id="test" onClick={()=>this.bindPopUpData("test")}>
Here I am calling the function to bind popup.
bindPopUpData=(dimension) =>{
return (
<div id="">
<div className="details_table">
<div className="close_popup">
<span className="icon-Close"></span>
</div>
</div>
</div>
);
}
-- Here I am trying to bind that popup
Can someone please tell where I am going wrong?
The issue is that your event handler is just returning a value, you're not telling the browser to put it anywhere.
Option 1: build the popup into the DOM with display:none and use your event handler to show/hide it.
Option 2: make your event handler inject the popup into the DOM with document.createElement and document.appendChild functionalities.
With React I'd recommend option 1 because you can make the popup a component and only render it if the parent component detects the event.
class myComponent extends React.Component {
constructor(props) {
super(props)
this.state = {
showpopup: false
}
this.myHandler = this.myHandler.bind(this)
}
myHandler () {
this.setState({showpopup: !showpopup})
}
render () {
return (
<section>
<span onClick={this.myHandler}></span>
{this.state.showpopup ? <Popup /> : ''}
</section>
)}
}

show only one popover at a time on state change

I want to show different popovers on different listitems using reactstrap but on state change the property of popover become true and show all popovers are displayed rather than just one that i clicked.
//setting popoveropen:false initially
this.state = {
popoverOpen: false
};
//on click this toggle function runs which change the popoverOpen state to true which displays all the popover
toggle() {
this.setState({
popoverOpen: !this.state.popoverOpen
})
}
//this is the jsx section where i have used popovers in list items on click of which the toggle function runs and change the popoverOpen State to true
render(){
return(
Members
Popover Title
body1
<ListGroupItem tag="button" id="Popover2" onClick=
{this.toggle2}>Labels<Popover placement="left" isOpen=
{this.state.popoverOpen}
target="Popover2" toggle={this.toggle}>
<PopoverHeader>Popover Title</PopoverHeader>
<PopoverBody>body2</PopoverBody>
</Popover>
</ListGroupItem>
</ListGroup>)}
just logic is missing in this code

inconsistent state and display in React when using browser 'back' button

I am using React 16.1.1 (with the react-rails gem) in a Rails 5.1 app.
The React components on a specific page work fine, except when going back to this page with the browser 'back' button (tested with firefox / chrome / safari). In that case, the display is inconsistent with the state of the component. I've setup a demo of the problem: https://lit-bastion-28654.herokuapp.com/.
Steps to reproduce:
be on /page1
click the 'selection mode' button, it sets 'selectionMode' to true
click 'page 2'
use 'back' button to go back to page 1
EXPECTED BEHAVIOUR: the button is grey (selectionMode is reset to false when component loaded). OBSERVED BEHAVIOUR: the button is still blue?!
There, you can see that the button is blue, as if selectionMode was true, but the react browser plugin shows that selectionMode is false. The React browser plugin shows false information: it shows that the button does not have the 'btn-primary' class (which is normal if selectionMode is false), but you can obviously see that in the DOM, it has the 'btn-primary' class, since it is appears blue.
This is my code:
page1.html.erb:
<%= react_component("EditableCardList", { editable: true }) %>
editable_card_list.js.jsx:
class EditableCardList extends React.Component {
constructor(props) {
super(props);
this.state = {
selectionMode: false
};
this.toggleSelectionMode = this.toggleSelectionMode.bind(this);
}
toggleSelectionMode() {
this.setState(prevState => ({ selectionMode: !prevState.selectionMode }));
}
render() {
if (this.props.editable === true)
return (
<div>
<div className="row card-buttons">
<div className="col-md-12">
<div className="pull-left">
<ManageCardsButtons
selectionMode={this.state.selectionMode}
toggleSelectionMode={this.toggleSelectionMode}
/>
</div>
</div>
</div>
</div>
)
}
}
manage_cards_buttons.js.jsx:
class ManageCardsButtons extends React.Component {
constructor(props) {
super(props);
}
render() {
return (
<span>
<button type="button" className={`btn btn-default btn-sm ${this.props.selectionMode ? 'btn-primary' : ''}`} onClick={this.props.toggleSelectionMode}>Selection mode</button>
{ this.props.selectionMode && <span>selection mode</span> }
</span>
)
}
}
So my question is: How to make sure that, after using 'back' in the browser, the button is rerendered properly (and appears grey instead of blue)?
The issue may be related with turbolinks and caching, but I've not been able to figure it out yet.
React and TurboLinks conflict, so my final solution was to disable TurboLinks caching on that specific page.
When you go back to page1, Component rerenders, and sets the default selectionMode which is false in your case.
you can use redux.
you can save selectionMode to your
localStorage when it changes, and by default load it from the
storage.

Resources