React.js: the most efficient way to pass a parameter to an event handler without bind() in a component

后端 未结 4 1971
借酒劲吻你
借酒劲吻你 2020-11-27 06:17

When an event handler uses this (juet like handleClick below uses this.setState), you have to bind the event handler with this

4条回答
  •  眼角桃花
    2020-11-27 06:38

    I used to do this kind of thing for state names. Depends on what you want, you can try this. So that, i don't have to bind the function again. Where you can get state name from e.target.name

    class App extends React.Component {
      state = {
        change: ""
      };
    
      handleChange = e => {
        const { name, value } = e.target;
        this.setState({ [name]: value  })
      };
    
      render() {
        return (
          
        );
      }
    }
    

提交回复
热议问题