简体   繁体   English

国家无法使用道具

[英]State can't access props

I am trying to add selectedUserIds in state constructor by using userListIds from props. 我试图通过使用道具的userListIds在状态构造函数中添加selectedUserIds However, it seems that at the time when constructor is executed the value in props is not set yet thus I am getting null. 但是,似乎在执行构造函数时,props中的值尚未设置,因此我得到了空值。

However, when I am printing this.props.userListIds at the beginning of the render it shows just fine. 但是,当我在渲染开始时打印this.props.userListIds时,它显示得很好。 Problem is somewhere else. 问题出在别的地方。 What might be the solution? 有什么解决方案? Here is the code: 这是代码:

 const mapStateToProps = (state: State, props: OwnProps): PropsFromState => ({
      userList: getCompanyTeamListSelector(state),
      userListIds: state.ddls.companyUsers.map(element => Number(element.id))
    });


@connect(mapStateToProps)
export default class EmailModal extends React.Component<OwnProps & Partial<PropsFromDispatch> & Partial<PropsFromState>, OwnState> {
  constructor(props: OwnProps) {
    super(props);
    this.state = {
      valueSelected: true, selectedUserIds: this.props.userListIds // HERE
    }}

You're doing React wrong. 您做错了React。 If the value can be accessed in the props, the value should stay in the props. 如果可以在道具中访问该值,则该值应保留在道具中。 Duplicating the data between the props and the state can lead to useless component re-rendering, and most importantly many bugs, especially when plugging libraries like React-Redux that will extensively manipulate the props. 在道具和状态之间复制数据会导致无用的组件重新渲染,最重要的是会导致许多错误,尤其是在插入像React-Redux这样的库时,它们会大量操纵道具。

Oh, and use props in the constructor, not this.props . 哦,在构造函数中使用props ,而不是this.props

Props is external data resource for a React component, so you should always assume that props data is not reliable, and take care of empty/undefined props in your component. 道具是React组件的外部数据资源,因此您应始终假定道具数据不可靠,并应注意组件中的空/未定义道具。

Given constructor function is executed before the first rendering in component lifecycle, it's possible that during the execution of constructor function, props.userListIds can be empty, and at render , it's retrieved and ready to be used. 给定构造函数在组件生命周期中的第一次渲染之前执行,则可能在构造函数执行期间props.userListIds可以为空,并且在render可以将其检索并准备使用。 If you really want to store userListIds in local state, you can use getDerivedStateFromProps to update local state: 如果您确实想以本地状态存储userListIds ,则可以使用getDerivedStateFromProps更新本地状态:

public static getDerivedStateFromProps(nextProps: IProps, prevState: IState) {
  return (nextProps.userListIds != prevState.selectedUserIds) ? 
    { selectedUserIds: nextProps.userListIds } :
    null
}

But same as other comments here, I am also against the idea of copying props to local state, since you will have 2 separated sources of truth for same piece of data and need to update local state whenever your props changes. 但是与这里的其他评论一样,我也反对将道具复制到本地状态的想法,因为对于相同的数据,您将有2个独立的真实来源,并且每当道具更改时都需要更新本地状态。 You can consider directly using props.userListIds instead. 您可以考虑直接使用props.userListIds

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

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