简体   繁体   English

ReactJS ajax 方法

[英]ReactJS ajax approach

I'm developing a SPA with React and Flux.我正在使用 React 和 Flux 开发 SPA。

Since now I've been following the approach of this article to tackle ajax requests: http://www.code-experience.com/async-requests-with-react-js-and-flux-revisited/从现在开始,我一直在按照本文的方法来处理 ajax 请求: http : //www.code-experience.com/async-requests-with-react-js-and-flux-revisited/

Nonetheless, I've found this other reference that uses a completely opposing approach: http://christianalfoni.github.io/javascript/2014/10/22/nailing-that-validation-with-reactjs.html尽管如此,我发现另一个使用完全相反的方法的参考: http : //christianalfoni.github.io/javascript/2014/10/22/nailing-that-validation-with-reactjs.html

The first one enforces heavy use of flux in every ajax request and the second one suggest the use of direct request from inside forms.第一个强制在每个 ajax 请求中大量使用通量,第二个建议使用来自内部表单的直接请求。

It is starting to seems to me that the use of flux add some unnecessary complexity to the code if the request could be done from within a component, like a .在我看来,如果请求可以从组件内部完成,例如 .

Of course if the request had relevant data to multiple pages/components a store should be used to transmit it.当然,如果请求具有与多个页面/组件相关的数据,则应使用存储来传输它。 But a simple form don't usually have this constraint.但是一个简单的表单通常没有这个限制。

Is there some reason to use flux all around or a mix of this two approaches is "architecturally acceptable"?是否有理由到处使用通量,或者这两种方法的混合是“架构上可接受的”?

The second approach is "wrong".第二种方法是“错误的”。 I put that in inverted commas because it clearly works and probably wouldn't get you in much trouble, but it is not fully Flux-like, IMO.我把它放在引号中,因为它显然有效并且可能不会给你带来太多麻烦,但它并不完全像 Flux,IMO。

Rather than using Ajax calls in components, it should fire off an action or action creator (depending on your flux implementation) and the request should happen there.而不是在组件中使用 Ajax 调用,它应该触发一个动作或动作创建者(取决于你的通量实现)并且请求应该在那里发生。 This lets you do stuff like updating stores, chaining actions and responding to errors, all in a way that will scale as your app gets bigger.这让您可以执行诸如更新商店、链接操作和响应错误之类的操作,所有这些都可以随着您的应用程序变大而扩展。

I think a lot of people over-worry about this kind of thing, and it's really only if you're working on a larger application that it'll become a pain point.我想很多人都过度担心这种事情,而且只有当你在开发一个更大的应用程序时,它才会成为一个痛点。 But if you want to follow the Flux way, my opinion is that the second approach isn't quite right.但是如果你想遵循 Flux 的方式,我的观点是第二种方法不太正确。

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

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