我有这个 Prop 界面:
interface OwnProps {
onHide: () => void;
show: boolean;
onSubmit: (props: SetCompanyUploaderFormSchema) => void;
client: Client;
}
还有这个 PropsFromState 接口(interface):
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
仍然显示为空数组。
但是,当我在 render()
开头打印 this.props.userListIds
时,它会正确显示。
问题出在哪里?
最佳答案
构造函数(props:propsFromState){}
然后调用 super(props);
关于javascript - 在状态构造函数中使用 PropsFromState 的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54076399/