简体   繁体   English

如何在Reactjs中绑定具有参数的构造函数中的函数

[英]How to bind function in constructor that has a parameter in Reactjs

So here's my function: 所以这是我的功能:

remove(element)
{   
    this.setState({ search: this.state.search.filter( item => item !== element ) }); 
}  

I get this error: 我收到此错误:

Warning: setState(...): Cannot update during an existing state transition (such as within `render` or another component's constructor). 
Render methods should be a pure function of props and state; constructor side-effects are an anti-pattern, but can be moved to `componentWillMount`.

If I have it set up like this: 如果我有这样设置:

constructor()
{
    this.remove = this.remove.bind(this);
}
render() 
{   
    return (
        <div>
            { this.state.search.map( (item, index) =>  
                (   
                    <button key={index} onClick={this.remove(item)}>{item.search}: {item.text}</button>
                ))  
            }   
            </div>
        </div>
    );
}

But if works fine if I remove the binding (well doesn't really matter) from the constructor and change the button line to this: 但是,如果我从构造函数中删除绑定(并没什么关系)并将按钮行更改为这样,则工作正常:

<button key={index} onClick={this.remove.bind(this, item)}>{item.search}: {item.text}</button>

So my question is, is there a way to bind it in the constructor so that it can take on the parameter? 所以我的问题是,有没有一种方法可以将其绑定到构造函数中,以便可以采用参数?

The difference between this.remove(item) and this.remove.bind(this, item) is that the first calls the function while the second creates a new function. this.remove(item)this.remove.bind(this, item)之间的区别在于,第一个调用函数,而第二个创建新函数。

So my question is, is there a way to bind it in the constructor so that it can take on the parameter? 所以我的问题是,有没有一种方法可以将其绑定到构造函数中,以便可以采用参数?

You can use this.remove.bind(this, item) and perform the binding the constructor, though it is unnecessary. 您可以使用this.remove.bind(this, item) 执行构造函数的绑定,尽管这是不必要的。

If you want to pass item to the event handler, then you have to create a new function in .map that can access item , with your current setup. 如果要将item传递给事件处理程序,则必须.map中创建一个新函数,该函数可以使用当前设置访问item This can be done via .bind or via a closure. 这可以通过.bind或通过闭包来完成。 In either case, binding in the constructor is simply not necessary. 无论哪种情况,都根本不需要在构造函数中进行绑定。

You can only avoid creating a new function if provide item in a different way, eg wrapping the button with another component that takes item as a prop (therefore pushing the function creation further down): 只有以其他方式提供item ,才可以避免创建新函数,例如,将按钮包装在另一个以该item为道具的组件上(因此将函数创建进一步推低):

function Button({item, onClick}) {
  return <button onClick={() => onClick(item)}>{item.search}: {item.text}</button>;
}

class Component extends React.Component {
  constructor()
  {
      this.remove = this.remove.bind(this);
  }

  render() 
  {   
      return (
          <div>
              { this.state.search.map( (item, index) =>  
                  (   
                      <Button key={index} onClick={this.remove} item={item} />
                  ))  
              }   
              </div>
          </div>
      );
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM