简体   繁体   English

反应-当道具改变时改变状态

[英]React - change state when props are changed

I have a React component, AttributeSingleChoice which I am calling like this: 我有一个React组件AttributeSingleChoice ,我这样调用:

Based on the new props I receive in it, I want to change its state, like this: 基于我收到的新props ,我想更改其状态,如下所示:

componentWillReceiveProps: function() {
    var attributeTypes = this.selectAttributes();
    this.setState({
        singleChoiceAttributes: attributeTypes});
},

selectAttributes: function() {
    return this.props.classification.attributes.map(function (elem) {
        if(elem.attributeType == "A") {
            return {description: elem.description, name: elem.name}
        }
    }).filter(Boolean);
},

However, if I use componentWillReceiveProps , state.props will remember the old props , not the new ones, as I would like. 但是,如果我使用componentWillReceivePropsstate.props将记住旧的props ,而不是我想要的新props

I tried using componentWillUpdate instead but I can't set the state inside componentWillUpdate . 我尝试改用componentWillUpdate但是无法在componentWillUpdate内设置状态。

How can I change the state of the component based upon new props? 如何基于新道具更改组件的状态?

The componentWillReceiveProps hook is passed the new props as an argument. componentWillReceiveProps挂钩传递给新的prop作为参数。

componentWillReceiveProps: function(newProps) {
  newProps !== this.props
}

You can accept an alternate props with a parameter on your selectAttributes function. 您可以在selectAttributes函数上接受带参数的替代道具。

selectAttributes: function(props) {
  // fallback to regular props if nothing passed
  props = props || this.props;

  return props.classification.attributes.map(function (elem) {
    // ...
  }).filter(Boolean);
}

Then pass the new props when they are available. 然后在可用时传递新道具。

componentWillReceiveProps: function(newProps) {
  var attributeTypes = this.selectAttributes(newProps);

  this.setState({
    singleChoiceAttributes: attributeTypes
  });
}

Your componentwillrecieveprops header is incorrect. 您的componentwillrecieveprops标头不正确。 You should take in a parameter for nextProps, which will contain the props being passed in. Then set your state variable based off of the nextProps. 您应该为nextProps输入一个参数,该参数将包含传入的道具。然后根据nextProps设置状态变量。 http://facebook.github.io/react/docs/component-specs.html#updating-componentwillreceiveprops http://facebook.github.io/react/docs/component-specs.html#updating-componentwillreceiveprops

You need to pass nextProps in to your function: 您需要将nextProps传递给函数:

componentWillReceiveProps: function( nextProps ) {
    var attributeTypes = this.selectAttributes( nextProps );
    this.setState({
        singleChoiceAttributes: attributeTypes});
},

selectAttributes: function( nextProps ) {
    var props = nextProps || this.props;
    return props.classification.attributes.map(function (elem) {
        if(elem.attributeType == "A") {
            return {description: elem.description, name: elem.name}
        }
    }).filter(Boolean);
},

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

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