简体   繁体   English

Redux-Saga的状态变更

[英]State changes in Redux-Saga

I have a simple React App. 我有一个简单的React App。 It allows a user to edit a form to update data in a database. 它允许用户编辑表单以更新数据库中的数据。 On Submit the page generates an action with the form data. 在“提交”页面上,将使用表单数据生成一个操作。 A Redux Saga yields to the action and asynchronously updates the database. Redux Saga屈服于该动作并异步更新数据库。 This all works. 所有这一切。

However in one case the update is slightly more involved. 但是,在一种情况下,更新会涉及更多一点。 Not only must new data be added but any data deleted on the form must be deleted from the database in a series of API calls. 不仅必须添加新数据,而且必须通过一系列API调用从数据库中删除在表单上删除的所有数据。 To do this I need to know what has changed (eg what has been deleted) rather than just the new state. 为此,我需要知道已更改的内容(例如已删除的内容),而不仅仅是新的状态。

How can my saga have access to this information? 我的传奇人物如何访问此信息? I could calculate it in the reducer because that has access to the previous state but it is commonly held to be an anti-pattern for the reducer to then dispatch a new action. 我可以在化简器中计算它,因为它可以访问先前的状态,但是通常认为它是化简器的反模式,然后可以分派新操作。

Sagas have a select effect available, which just runs a selector function and returns the extracted state. Sagas具有select效果,该效果仅运行选择器功能并返回提取的状态。 You can use this to retrieve the old and new items from the Redux store, and deal with the changes from there: 您可以使用它从Redux商店中检索旧项目和新项目,并从那里处理更改:

function* handleFormUpdates() {
    const oldItem = yield select(selectOldItem);
    const newItem = yield select(selectNewItem);

    const changes = diffTheItems(oldItem, newItem);

    // make API calls to deal with changes appropriately
}

Overall, this is a good reason to keep the "temporary" or "draft" state in Redux, so that you can make use of both the "current" and "draft" values in your logic. 总的来说,这是在Redux中保持“临时”或“草稿”状态的一个很好的理由,这样您就可以在逻辑中同时使用“当前”和“草稿”值。

I discussed some related concepts in my blog posts Practical Redux, Part 7: Form Change Handling, Data Editing, and Feature Reducers and Practical Redux, Part 8: Form Draft Data Management 我在自己的博客文章《 实用的Redux,第7部分:表单更改处理,数据编辑以及功能减少者实用的Redux,第8部分:表单草稿数据管理》中讨论了一些相关概念。

...any data deleted on the form must be deleted from the database in a series of API calls. ...必须通过一系列API调用从数据库中删除在表单上删除的所有数据。 To do this I need to know what has changed (eg what has been deleted) rather than just the new state. 为此,我需要知道已更改的内容(例如已删除的内容),而不仅仅是新的状态。

If I understand correctly you have form state saved in a redux store and you need to know when and what has changed. 如果我正确理解,您将表单状态保存在redux存储中,那么您需要知道何时以及发生了什么更改。 You could create your own watcher saga: 您可以创建自己的观察者传奇:

function* watchFormUpdates(){
  while (true) {
    const oldFormState = yield select(selectors.selectFormState);
    const action = yield take (actionTypes.FORM_UPDATE); // wait until action is dispatched
    const newFormState = yield select(selectors.selectFormState); // at this point store has been updated
    // compare oldFormState with newFormState...
    if(oldFormState.hasSubscription && !newFormState.hasSubscription) {
      yield fork(deleteSubscriptionSaga); // start non-blocking worker task
      // or just dispatch action - yield put(actionCreators.deleteSubscription());
    }
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM