Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Warning: This synthetic event is reused for performance reasons happening with <input type="checkbox" />

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.

Warning Error Image

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 :)

like image 298
Raymi Avatar asked Mar 26 '18 20:03

Raymi


People also ask

What is correct about synthetic event?

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.

What does synthetic event mean?

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.

What is event persist () React?

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.

What are synthetic events in React native?

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).


2 Answers

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     }   })); }; 
like image 183
w35l3y Avatar answered Sep 24 '22 17:09

w35l3y


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   }); }; 
like image 37
Adam Orłowski Avatar answered Sep 22 '22 17:09

Adam Orłowski