目的想把异步获取的一个数据 sourceId 经过加工 (isNet = sourceId > 3 ? 1 : 0 ),通过 setState 赋值给 state 中的 isNet 字段;
刚开始的时候设置在 componentWillReceiveProps 生命周期中:
componentWillReceiveProps(nextProps: Readonly<CustomerFormProps>): void {
const { CustomerEditService: { sourceId } } = nextProps;
if (sourceId === this.props.CustomerEditService.sourceId) {
return
}
this.setState({ isNet : sourceId > 3 ? 1 : 0 });
}
结果在 render 中设置 select 的 defaultValue 并没有产生效果:
经过查询文档将设置放在 componentWillMount 生命周期中,效果实现。
componentWillMount(): void {
const { CustomerEditService: { sourceId } } = this.props;
const isNet = sourceId > 3 ? 1 : 0;
this.setState({ isNet });
}
<Col lg={6} md={24} style={{ paddingLeft: 0 }}>
<Form.Item label={fieldLabels.channel}>
{/* 是否网络客户选择 */}
<Select defaultValue={ isNet } onChange={value => this.onSelectNet(value)}>
{ csr_src_type.son.map( (item, index) => <Option key={ item.id } value={ index }> { item.name } </Option>, ) }
</Select>
</Form.Item>
</Col>
网友评论