React componentWillReceiveProps
在React Redux 开发场景下出现一个特殊情况:
在model中未定义的字段,通过 compcomponentWillReceiveProps 获得值是永远是相同的,且是异步获得值;
const Model: ModelType = {
namespace: 'CustomerEditService',
state: {
···
},
effects: {*fetch({ payload }, { call, put }) {
const response = yield call(queryCustomer, payload);
yield put({ type: 'save', payload: response.data.data, });
},},
reducers: {
save(state, action) {
return { ...state, ...action.payload, };
}
}
}export default Model;
// eslint-disable-next-line react/sort-
compcomponentWillReceiveProps(nextProps: Readonly<CustomerFormProps>): void {
const { dictionaries: { area }, CustomerEditService: { sourceId } } = nextProps;
if (sourceId === this.props.CustomerEditService.sourceId) {
return
}
this.setState({ countries: area });
}