Reactjs 从react js中的道具更新ui。react中道具到状态的切换

Reactjs 从react js中的道具更新ui。react中道具到状态的切换,reactjs,redux,state,Reactjs,Redux,State,当我的道具在reactjs和redux构建中更改时,我很难弄清楚如何更新我的UI。这似乎是一个相对正常和简单的问题,但我正在努力解决它。我发现react关于这方面的文档非常不充分(一段简短的解释,没有示例)。我还希望看到新的和正确的es6格式。 据我所知,我可以设置我的道具并系上它们,这样它们就能更新我的状态。我设置了初始道具,因此请求的道具永远不会为空。我对状态也做了同样的操作,但它们都没有出现在渲染或状态树中 我找到了这个设置我的初始状态,并使用它 export default class

当我的道具在reactjs和redux构建中更改时,我很难弄清楚如何更新我的UI。这似乎是一个相对正常和简单的问题,但我正在努力解决它。我发现react关于这方面的文档非常不充分(一段简短的解释,没有示例)。我还希望看到新的和正确的es6格式。 据我所知,我可以设置我的道具并系上它们,这样它们就能更新我的状态。我设置了初始道具,因此请求的道具永远不会为空。我对状态也做了同样的操作,但它们都没有出现在渲染或状态树中

我找到了这个设置我的初始状态,并使用它

export default class MyComponent extends React.Component{
  constructor(props){
    super(props);
    this.state = { userBirthdate: '1/2/1982', };
  }
}
我发现这是为了使用componentWillReceiveProps()更新ui

但是,当我查看react检查器时,我的容器没有状态(在状态下的检查器中显示“Empty object”消息)。道具正在更新并反映我在redux中的用户选择。当我选择一个新元素时,它会反映在redux中,并填充我正在使用的正确道具,但它们不会传递到状态


非常感谢您的帮助。

看来生命周期功能
shoulldcomponentupdate
将是您的最佳选择

export default class MyComponent extends React.Component{
  constructor(props){
    super(props);
    this.state = { userBirthdate: '1/2/1982', };
  }
  shouldComponentUpdate(nextProps, nextState) {
    if(this.props.userBirthdate !== nextProps.userBithdate) {
       return true;
    }
  }
}

如果所需的数据是通过道具输入的,则不必担心将其“同步”到状态,除非您需要在组件中对其进行操作

相反,只需直接使用道具,例如:

export default class MyComponent extends React.Component{

  static defaultProps = {
    userBirthdate: '1/2/1982'
  };

  render() {
    const { userBirthdate } = this.props;
    return <span>DOB: {userBirthdate}</span>;
  }
}
导出默认类MyComponent扩展React.Component{
静态defaultProps={
用户出生日期:“1982年1月2日”
};
render(){
const{userBirthdate}=this.props;
返回DOB:{userBirthdate};
}
}
export default class MyComponent extends React.Component{

  static defaultProps = {
    userBirthdate: '1/2/1982'
  };

  render() {
    const { userBirthdate } = this.props;
    return <span>DOB: {userBirthdate}</span>;
  }
}