我有这个道具界面:
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
设置为PropsFromState
值userListIds
。我试图在构造函数中做到这一点:
@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
。除此之外,我尝试使用
getDerivedStateFromProps
和ComponentWillMount
进行设置。到目前为止没有任何效果,this.state.selecteduserIds
仍然显示为空数组。但是,当我在
this.props.userListIds
的开头打印render()
时,它可以正确显示。问题出在哪里?
最佳答案
构造函数(props:propsFromState){}
然后调用super(props);
关于javascript - 在状态构造函数中使用PropsFromState中的值,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/54076399/