avoid constant re-render from “input” or “textarea” in react js

前端 未结 4 1453
面向向阳花
面向向阳花 2021-01-11 14:32

Currently in react js, when I want to bind a text area or an input with a \"state\", I will need to set the onChange method and setState() everytime user type in a single le

4条回答
  •  栀梦
    栀梦 (楼主)
    2021-01-11 14:48

    You need to bind the onChange() event function inside constructor like as code snippets :

    class MyComponent extends Component {
      constructor() {
        super();
        this.state = {value: ""};
        this.onChange = this.onChange.bind(this)
      }
    
      onChange= (e)=>{
        const formthis = this;
        let {name, value} = e.target;
    
        formthis.setState({
          [name]: value
        });
      }
    
      render() {
        return (
          
    ); } }

提交回复
热议问题