在状态构造函数中使用PropsFromState中的值

时间:2019-01-07 14:36:50

标签: javascript reactjs react-redux

我有这个道具界面:

interface OwnProps {
  onHide: () => void;
  show: boolean;
  onSubmit: (props: SetCompanyUploaderFormSchema) => void;
  client: Client;
}

此PropsFromState接口:

interface PropsFromState {
  userList: DropDownListItem<string>[];
  userListIds: Array<number>;
}

我这样初始化:

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

我正在尝试将状态值selectedUsersIds设置为PropsFromStateuserListIds

我试图在构造函数中这样做:

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

我也尝试过this.props.userListIds

除此之外,我还尝试使用getDerivedStateFromPropsComponentWillMount进行设置。到目前为止,什么都没做,this.state.selecteduserIds仍然是空数组。

但是,当我在this.props.userListIds的开头打印render()时,它可以正确显示。

问题出在哪里?

1 个答案:

答案 0 :(得分:1)

constructor(props:propsFromState){}

然后调用super(props);