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

后端 未结 5 752
故里飘歌
故里飘歌 2021-01-31 07:25

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

5条回答
  •  囚心锁ツ
    2021-01-31 08:15

    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
        }
      }));
    };
    

提交回复
热议问题