如何在调度操作后更新组件的本地状态?
就我而言,我显示了一个基于组件本地状态的 popin:
<button onClick={() => this.setState({ popin: true })}>Open</button>
<Popin hidden={!this.state.popin}>
<form onSubmit={createItem})>
<div className="popin-heading">...</div>
<button onClick={() => this.setState({ popin: false })}>Close</button>
<button type="submit">Submit</button>
</form>
</Popin>
Run Code Online (Sandbox Code Playgroud)
在提交单击时,在 Saga 中 createItem 调度动作捕获:
function* watchCreateItem() {
yield takeEvery('CREATE_ITEM', doCreateItem);
}
function* doCreateItem(values) {
try {
// Do POST API request
const response = yield fetch('/create', { method: 'post', body: values });
// Disptach action to store new item in redux store (by reducer)
yield put(storeItem(response));
/**
* !!! Here, want to …Run Code Online (Sandbox Code Playgroud)