score:374
Looks like you're accidentally calling the handleButtonChange
method in your render method, you probably want to do onClick={() => this.handleButtonChange(false)}
instead.
If you don't want to create a lambda in the onClick handler, I think you'll need to have two bound methods, one for each parameter.
In the constructor
:
this.handleButtonChangeRetour = this.handleButtonChange.bind(this, true);
this.handleButtonChangeSingle = this.handleButtonChange.bind(this, false);
And in the render
method:
<Button href="#" active={!this.state.singleJourney} onClick={this.handleButtonChangeSingle} >Retour</Button>
<Button href="#" active={this.state.singleJourney} onClick={this.handleButtonChangeRetour}>Single Journey</Button>
score:0
The solution that I use to open Popover for components is reactstrap (React Bootstrap 4 components).
class Settings extends Component {
constructor(props) {
super(props);
this.state = {
popoversOpen: [] // array open popovers
}
}
// toggle my popovers
togglePopoverHelp = (selected) => (e) => {
const index = this.state.popoversOpen.indexOf(selected);
if (index < 0) {
this.state.popoversOpen.push(selected);
} else {
this.state.popoversOpen.splice(index, 1);
}
this.setState({ popoversOpen: [...this.state.popoversOpen] });
}
render() {
<div id="settings">
<button id="PopoverTimer" onClick={this.togglePopoverHelp(1)} className="btn btn-outline-danger" type="button">?</button>
<Popover placement="left" isOpen={this.state.popoversOpen.includes(1)} target="PopoverTimer" toggle={this.togglePopoverHelp(1)}>
<PopoverHeader>Header popover</PopoverHeader>
<PopoverBody>Description popover</PopoverBody>
</Popover>
<button id="popoverRefresh" onClick={this.togglePopoverHelp(2)} className="btn btn-outline-danger" type="button">?</button>
<Popover placement="left" isOpen={this.state.popoversOpen.includes(2)} target="popoverRefresh" toggle={this.togglePopoverHelp(2)}>
<PopoverHeader>Header popover 2</PopoverHeader>
<PopoverBody>Description popover2</PopoverBody>
</Popover>
</div>
}
}
score:1
I got the same error when I was calling
this.handleClick = this.handleClick.bind(this);
in my constructor when handleClick didn't exist
(I had erased it and had accidentally left the "this" binding statement in my constructor).
Solution = remove the "this" binding statement.
score:1
The problem is certainly the this binding while rending the button with onClick handler. The solution is to use arrow function while calling action handler while rendering. Like this:
onClick={ () => this.handleButtonChange(false) }
score:1
The onClick function must pass through a function that returns the handleButtonChange() method. Otherwise it will run automatically, ending up with the error/warning. Use the below to solve the issue.
onClick={() => this.handleButtonChange(false)}
score:2
From react docs Passing arguments to event handlers
<button onClick={(e) => this.deleteRow(id, e)}>Delete Row</button>
<button onClick={this.deleteRow.bind(this, id)}>Delete Row</button>
score:2
This same warning will be emitted on any state changes done in a render()
call.
An example of a tricky to find case:
When rendering a multi-select GUI component based on state data, if state has nothing to display, a call to resetOptions()
is considered state change for that component.
The obvious fix is to do resetOptions()
in componentDidUpdate()
instead of render()
.
score:4
If you are trying to add arguments to a handler in recompose
, make sure that you're defining your arguments correctly in the handler. It is essentially a curried function, so you want to be sure to require the correct number of arguments. This page has a good example of using arguments with handlers.
Example (from the link):
withHandlers({
handleClick: props => (value1, value2) => event => {
console.log(event)
alert(value1 + ' was clicked!')
props.doSomething(value2)
},
})
for your child HOC and in the parent
class MyComponent extends Component {
static propTypes = {
handleClick: PropTypes.func,
}
render () {
const {handleClick} = this.props
return (
<div onClick={handleClick(value1, value2)} />
)
}
}
this avoids writing an anonymous function out of your handler to patch fix the problem with not supplying enough parameter names on your handler.
score:6
THE PROBLEM is here: onClick={this.handleButtonChange(false)}
When you pass this.handleButtonChange(false)
to onClick, you are actually calling the function with value = false
and setting onClick to the function's return value, which is undefined. Also, calling this.handleButtonChange(false)
then calls this.setState()
which triggers a re-render, resulting in an infinite render loop.
THE SOLUTION is to pass the function in a lambda: onClick={() => this.handleButtonChange(false)}
. Here you are setting onClick to equal a function that will call handleButtonChange(false) when the button is clicked.
The below example may help:
function handleButtonChange(value){
console.log("State updated!")
}
console.log(handleButtonChange(false))
//output: State updated!
//output: undefined
console.log(() => handleButtonChange(false))
//output: ()=>{handleButtonChange(false);}
score:14
That usually happens when you call
onClick={this.handleButton
()}
- notice the () instead of:
onClick={this.handleButton
} - notice here we are not calling the function when we initialize it
score:17
I am giving a generic example for better understanding, In the following code
render(){
return(
<div>
<h3>Simple Counter</h3>
<Counter
value={this.props.counter}
onIncrement={this.props.increment()} <------ calling the function
onDecrement={this.props.decrement()} <-----------
onIncrementAsync={this.props.incrementAsync()} />
</div>
)
}
When supplying props I am calling the function directly, this wold have a infinite loop execution and would give you that error, Remove the function call everything works normally.
render(){
return(
<div>
<h3>Simple Counter</h3>
<Counter
value={this.props.counter}
onIncrement={this.props.increment} <------ function call removed
onDecrement={this.props.decrement} <-----------
onIncrementAsync={this.props.incrementAsync} />
</div>
)
}
Source: stackoverflow.com
Related Query
- React Native / Redux - setState - Cannot update during an existing state transition
- ReactJS | Cannot update during an existing state transition
- React: Cannot update during an existing state transitio
- Updating What Function is returning Which is Inside Render Warning: Cannot update during an existing state transition reactjs
- Reactjs Cannot Update During an Existing State Transition
- ReactJS: Warning: setState(...): Cannot update during an existing state transition
- Why does this error exist: "Invariant Violation: Cannot update during an existing state transition"
- Cannot update during an existing state transition (such as within `render`). Render methods should be a pure function of props and state. in Index.js
- Warning: setState(...): Cannot update during an existing state transition with redux/immutable
- Cannot update during an existing state transition in stateless component
- Cannot update during an existing state transition error in React
- Warning: setState(…): Cannot update during an existing state transition
- Cannot update during an existing state transition - Not using any illegal setState()
- ES6 - Warning: setState(…): Cannot update during an existing state transition
- ReactJS: Cannot update during an existing state transition (such as within `render`). Render methods should be a pure function of props and state
- React: Cannot update during an existing state transition (such as within `render`). Render methods should be a pure function of props and state
- setstate cannot update during an state transition - react
- Cannot update during an existing state transition error
- RX + React setState - avoid “Cannot update during an existing state transition”?
- Timer class based, Warning: Cannot update during an existing state transition (such as within `render`)
- Incremental React Game with setInterval: Cannot update during existing state transition
- Know what is causing “Warning: setState(…): Cannot update during an existing state transition…”, but not sure where the cause should be moved to
- index.js:1335 Warning: setState(...): Cannot update during an existing state transition (such as within `render` or another component's constructor).
- React Warning: Cannot update during an existing state transition (such as within `render`)
- Error: Warning: setState(...): Cannot update during an existing state transition (such as within `render` or another component's constructor)
- Cannot update during an existing state transition (such as within `render`). When calling onChange function in antd Select
- React: Apollo Client: Cannot update during an existing state transition
- React Warning: Cannot update during an existing state transition
- Warning: setState(...): Cannot update during an existing state transition in react redux
- Cannot update during an existing state transition
More Query from same tag
- How to solve Uncaught TypeError: Cannot read properties of undefined (reading 'map') in React
- How can I import the const data from another file using Promise (React)
- React-Native Invariant Violation: Element type is invalid
- react routing with parcel and router dom 6.3
- Getting an arrow function syntax error in React
- Accordion SideBar Menu using Nav components with react-bootstrap
- How to access dragged object fields in react drag and drop?
- React.js - Drawer covers page conent
- React Flask Heroku App is not displaying frontend
- React, Enzyme async lifecycle function
- How to prevent click release on different element
- How to import multi images in ReactJS
- Typescript error when typing onChange event
- Select option child component not reseting to key 0 on form submit with reactjs
- How do i get signed in users information in with reactjs and mongodb?
- React The action 'NAVIGATE' with payload {"name":""} was not handled by any navigator
- Building react library using Rollup, How to avoid webpack alias in next.config.js
- Using Webpack alias cause to ESLint error on import alias in WebStorm
- React & Typescript: nested defaultProps deep merge
- handleSubmit and onChange handleSubmit resulting in setState error or cannot enter character
- Environment Variables are not showing up in my Context component, on Nextjs. Would I need to configure Nextjs? Or set up Context to use the variables?
- ReactJS how to apply transitions to inline stylings in react?
- React.js transitions not working for "fade out"
- Not able to move my marker using the library leaflet
- ES6 and webpack: Redux: How to import a file from a distant directory
- Dynamically updating index.html file in production build of a create react app
- Why does this error exist: "Invariant Violation: Cannot update during an existing state transition"
- naming convention for react hooks and props?
- FieldArray component does not work
- react on datepicker click want to change toggle value