我有这个道具界面:
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()
时,它可以正确显示。
问题出在哪里?
答案 0 :(得分:1)
constructor(props:propsFromState){}
然后调用super(props);