简体   繁体   English

整合React,Redux,Redux-Form和React-Saga的最佳方法?

[英]Best way to integrate React, Redux, Redux-Form, and React-Saga?

I have read everything I can find on how to use react-saga and redux-form together I'm stuck at a crossroads and need some advice. 我已经阅读了有关如何将react-saga和redux-form一起使用的所有信息,我陷在十字路口,需要一些建议。

Here is how I ended up here. 这就是我最后的结果。

I chose to work with Redux because it makes sense for my app and I don't have any problems there. 我选择与Redux合作,因为它对我的应用程序有意义,而且在那里我没有任何问题。

Switch from Thunk to Sagas 从Thunk切换到Sagas

I started working on my async api calls to populate my app with data. 我开始处理异步api调用,以用数据填充我的应用程序。 I'm using a lot of data grids and I'm not sure where I'll end up on latency as some of the queries are quite complex. 我使用了大量的数据网格,由于某些查询非常复杂,因此我不确定在哪里会出现延迟。

When I started working on the action creators and methods for doing background polling of data I realized that Sagas were going to be much easier than Thunk. 当我开始研究动作创建者和进行数据背景调查的方法时,我意识到Sagas比Thunk容易得多。

I made that switch and I'm happy with it. 我做出了切换,对此感到满意。 Handling any errors from the api calls is being handled by an action creator that updates redux state container for all of my api calls. 操作创建者正在处理来自api调用的任何错误,该操作创建者会为我的所有api调用更新redux状态容器。 Errors are displayed by populating a modal based on those state changes. 通过基于这些状态更改填充模态来显示错误。

Introduciton of Redux-Form Redux-Form简介

Once I started working on posting form data back to the api things got interesting and I realized I was about to write a lot of code to handle it all through Redux. 一旦我开始将表单数据发布回api,事情就会变得很有趣,并且我意识到我将要编写大量代码来通过Redux处理所有这些。 Redux-form simplified things as it has a state container for everything form related and makes it really easy to setup and validate forms on the client side. Redux-form简化了事情,因为它具有用于所有与表单相关的状态容器,并且使在客户端设置和验证表单变得非常容易。

Redux-form handles all of the form state in my configuration until the form is submitted. Redux-form处理我配置中的所有表单状态,直到提交表单为止。

I'm using a container component conected to a Redux store I created to hold state of api requests. 我正在使用连接到Redux存储的容器组件,该存储组件是我创建的,用于保存api请求的状态。

The form is a child component of the container that is connected to Redux through redux-form which handles all of it's state. 该表单是容器的子组件,该容器通过处理所有状态的redux-form连接到Redux。

When the form is submitted I am calling a Redux action which in turn called a saga to post the data. 提交表单后,我正在调用Redux操作,该操作又调用了一个传奇来发布数据。

The result of the saga api call is to either dispatch a success or failure action in Redux. saga api调用的结果是在Redux中调度成功或失败操作。 I am passing an object to the api state that contains the status (success/failure), an error object with any errors, and a return object where I can return things like the id's of records just created. 我正在将一个对象传递到api状态,该状态包含状态(成功/失败),一个具有任何错误的错误对象以及一个返回对象,在这里我可以返回诸如刚创建的记录的ID之类的内容。

That works well when the error from the api is communication related. 当api的错误与通信有关时,这种方法效果很好。 Because I'm updating the api state my higher level app components have access to it so I can do things like trigger a modal for errors not specifically related to the form data itself. 因为我正在更新api状态,所以我的高级应用程序组件可以访问它,因此我可以执行一些操作,例如触发与模式数据本身不相关的错误的模式。

But when I started thinking of how I would handle any field errors that could occur if the client side validation was missing some logic I got lost. 但是,当我开始考虑如果客户端验证缺少某些逻辑时我将如何处理可能发生的任何字段错误,我就迷失了。

In my current setup those errors would be on the api state object. 在我当前的设置中,这些错误将在api状态对象上。 I could put them into a modal but there would be no client side error handling on the form itself. 我可以将它们放入模式,但在表单本身上没有客户端错误处理。

Redux-form can handle server side validation tied directly back to the form fields but only from a promise and from what I can tell trying to return a promise through action creators would be difficult if not impossible. Redux-form可以处理直接绑定到form字段的服务器端验证,但是只能从一个Promise中进行,而根据我的判断,通过动作创建者返回Promise将是困难的,即使不是不可能。

I can write a promise in my onSubmit function but I would have to call my saga function directly instead of triggering it through an action. 我可以在onSubmit函数中写一个promise,但是我必须直接调用saga函数,而不是通过操作来触发它。 Is that an acceptable pattern? 这是可以接受的模式吗?

I guess I could trigger an action from the saga to populate my api state values but it seems backwards. 我想我可以从传奇中触发一个动作来填充我的api状态值,但它似乎是倒退的。

Basically I would prefer to handle comm errors one way (through my api state container) and form field data errors another way (back throu redux-form and it's error handlers) and I'm not sure which direction to take. 基本上,我更喜欢以一种方式(通过我的api状态容器)处理comm错误,而以另一种方式(通过redux-form及其错误处理程序)处理表单数据错误,而且我不确定该朝哪个方向进行。

I looked at a module redux-form-saga which makes it possible to return a promise directly back to the form and therefore use the redux-form error handling after the api call but I'm not sure if I would be able to also trigger my api state actions at the same time. 我看了一个模块redux-form-saga,它可以直接将诺言返回到表单,因此可以在api调用后使用redux-form错误处理,但是我不确定是否也可以触发同时执行我的api状态操作。

Rather than continuing going down the rabbit hole and maybe over complicating things I thought I would solicit some advice from anyone who has had to deal with something similar. 我认为我不会向那些不得不处理类似问题的人征求一些建议,而不是继续钻研兔子的孔,也许不是使事情复杂化。

I'm good with async background calls to populate my data grids but when I have to post data back to the api I want to make sure the user can't take any other actions until they get a response back. 我对使用异步后台调用来填充数据网格很满意,但是当我不得不将数据发布回api时,我想确保用户在收到响应之前不能采取任何其他操作。

This is the first section of many in this app so I want to create a design pattern that makes sense, is easily reproducible, is reliable, and easy to follow. 这是该应用程序中许多内容的第一部分,因此我想创建一个有意义,易于复制,可靠且易于遵循的设计模式。

Any suggestions? 有什么建议么?

React-Boilerplate will help you integrate React, redux, redux-saga. React-Boilerplate将帮助您集成React,redux,redux-saga。 On top of that integrating redux-form should be straightforward. 最重要的是,集成redux-form应该很简单。 React-Boilerplate uses all the current best practices of the community for a production ready app React-Boilerplate使用社区的所有当前最佳实践来生产可就绪的应用程序

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

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