代码之家  ›  专栏  ›  技术社区  ›  Hemadri Dasari

用参数处理事件处理函数的更好方法是什么?

  •  1
  • Hemadri Dasari  · 技术社区  · 8 年前

    我试图实现事件处理函数,以避免每次组件 renders re-renders .

    场景1:

    如果我在构造函数中绑定函数,如下所示,并且在 onChange 它只在bundle文件中创建一个新函数一次

     constructor(props){
        super(props);
        this.handleChange = this.handleChange.bind(this);
        this.state = {
           value: ""
        }
     } 
    
     handleChange(e){
        this.setState({
           value: e.target.value
        });
     }
    
     render(){
        const { value } = this.state;
        return(
          <div>
            <input type="text" onChange={this.handleChange} value={value} />
          </div>
        )
     }
    

    场景2:

    但当我想通过 some parameters 随着 event 到 handleChange 呈现 和 重新呈现

     constructor(props){
        super(props);
        this.handleChange = this.handleChange.bind(this);
        this.state = {
           value: ""
        }
     } 
    
     handleChange(e, param1, param2){
        this.setState({
           value: e.target.value
        });
     }
    
     render(){
        const { value } = this.state;
        return(
          <div>
            <input type="text" onChange={e => this.handleChange(e, param1, param2)} value={value} />
          </div>
        )
     }
    

    所以,

    只在bundle文件中一次,而不是每次组件呈现和重新呈现时?有可能吗?

    编辑:

    param1和param2是我自己的自定义值。

    1 回复  |  直到 8 年前
        1
  •  1
  •   Ori Drori    8 年前

    如果目标组件可以传递多个参数( input 显然不是),您的第二个场景不需要箭头函数就可以工作:

     render(){
        const { value } = this.state;
        return(
          <div>
            <CustomInput type="text" onChange={this.handleChange} value={value} />
          </div>
        )
     }
    

    例子:

    class CustomInput extends React.Component {
      constructor(props) {
        super(props);
        
        this.inputHandleChange = this.inputHandleChange.bind(this);
      }
      
      inputHandleChange(e) {
        this.props.onChange(e, e.target.value.length, 'param2');
      }
      
      render() {
        return (
          <input {...this.props} onChange={this.inputHandleChange} />
        );
      }
    }
    
    class InputWrapper extends React.Component {
      constructor(props) {
        super(props);
        this.handleChange = this.handleChange.bind(this);
        this.state = {
          value: ""
        }
      }
    
      handleChange(e, param1, param2) {
        console.log(e.target.value, param1, param2);
        
        this.setState({
          value: e.target.value
        });
      }
    
      render() {
        const {
          value
        } = this.state;
        return (
          <div>
            <CustomInput type="text" onChange={this.handleChange} value={value} />
          </div>
        )
      }
    }
    
    ReactDOM.render(
      <InputWrapper />,
      demo
    );
    <script crossorigin src="https://unpkg.com/react@16/umd/react.development.js"></script>
    <script crossorigin src="https://unpkg.com/react-dom@16/umd/react-dom.development.js"></script>
    
    
    <div id="demo"></div>
    推荐文章