I've been working on a simple react-redux todo example for a class and I came across several warning messages that show in the console everytime I check and uncheck a checkbox input.
You can see the warnings in the following images.
I also did a google search for the warning message but couldn't find any solution that works. Also, what stroke my attention was that it looks like it was trying to access every property of the native event, and DOM element.
This is the code for the presentational component that has the input checkbox
class TodoItem extends React.Component { state = { isChecked: false }; handleCheckbox = () => { this.setState({ isChecked: !this.state.isChecked }); }; render() { const { todos, onItemClick } = this.props; const { isChecked } = this.state; return ( <div> <ul> {todos.map((todo, id) => { return ( <li key={id} onClick={onItemClick}> <input onChange={this.handleCheckbox} type="checkbox" checked={isChecked} /> <label> <span /> {todo.textInput} </label> </li> ); })} </ul> </div> ); } } export default TodoItem;
I uploaded the example on CodeSandbox as well: https://codesandbox.io/s/k0mlxk1yqv
If you want to replicate this error you need to add an Item to the todo List and click the checkbox to check and uncheck a couple of times.
If anyone has any idea why this warning signs keep appearing and how to disable them I would appreciate your input very much :)
Your event handlers will be passed instances of SyntheticEvent , a cross-browser wrapper around the browser's native event. It has the same interface as the browser's native event, including stopPropagation() and preventDefault() , except the events work identically across all browsers.
Synthetic events are event objects that are created ex nihilo by. the JavaScript programmer, as opposed to "real" events that come into. being as a result of an action within the browser window. Synthetic. events can, for example, simulate a selection from a drop-down list box.
persist() . React uses the SyntheticEvent objects to wrap native events. For performance reasons, synthetic events are pooled and reused across multiple native events. To assure consistent usage of the pooled events, React nullifies the properties of synthetic events right after executing an event handler.
SyntheticEvent is a React specific wrapper around native events, that essentially have the same interface as the browser events you're already used to, including stopPropagation() and preventDefault() (for more information, the official documentation on events has a dedicated page here).
This happened because the event
implicitly passed to onItemClick
is used in an asynchronous context.
As Andre Lemay said, you should assign your needs to local variables and reference them.
In my case, I had this code:
handleInput = e => { // <-- e = synthetic event this.setState(state => ({ // <-- asynchronous call data: { ...state.data, [e.target.name]: e.target.value // <-- this was causing the warnings (e.target is in an asynchronous context) } })); };
Then I changed it to:
handleInput = e => { const { name, value } = e.target; // <-- moved outside asynchronous context this.setState(state => ({ data: { ...state.data, [name]: value } })); };
I'd suggest trying two solutions:
First change
onChange={this.handleCheckbox}
to
onChange={() => this.handleCheckbox()}
If that won't work, in 'handleCheckbox' add event.persist();
Like this:
handleCheckbox = (event) => { event.persist(); this.setState({ isChecked: !this.state.isChecked }); };
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With