首页 > 解决方案 > Is Redux.js a sufficient way to separate data from view in a React.js/Node.js hybrid application?

问题描述

I am quite new to web programming. I've started developing a single-page web application using React.js with a Node server. I've read the tutorial, played with boilerplates, and quickly I understood React would take care only of the view aspect. So I tried to put my data-processing functions with the export keyword in a JavaScript file so I could use them in my React components. But that way was pretty "dirty" and it didn't feel satisfying at all.

Then I looked for a way to effectively separate the model and the controller from the view, so that I could completely change the GUI with little effort, and thus allow the project to grow and multiple people to work on it at the same time.

I've came across this article explaining the Flux architecture, and I saw a major implementation to use with React.js was Redux.js. I was quite surprised that I didn't see it at first, and now I wonder how much frameworks one has to use when working with JavaScript and Web.

My question is simple : is Redux.js all I need to effectively separate data, treatments and GUI components ? Or did I miss something else ? Are there any other major architectures you would recommend ?

Many thanks,

标签: javascriptnode.jsreactjsredux

解决方案


Redux 通常用于包含您的应用程序需要的所有数据。它就像一个商店,可以将所需的数据传递给实际需要数据的组件。例如,如果您需要从 ajax 请求中获取的特定数据分布在两个组件之间,那么 redux 是一个完美的选择。是否需要 redux 取决于您的应用程序的结构。如果您有一个包含大量组件的应用程序,并且其中大多数都需要来自您的服务器或 api 的数据,我建议您使用 redux。一旦你学会了它,它就非常简单。是的,你只需要 redux 就可以将数据从 ui 中分离出来。要获得格式良好的结构,请将所有 ajax 请求放在一个文件夹下并将其导出,以便您可以从 ui 组件中调用它。当您收到数据时,将有效负载传递给 redux 存储,该存储将自动将其传递给连接到该特定减速器的所有组件。有关如何使用 redux 连接 react 的详细信息,请查看他们的文档: https://redux.js.org/basics/usage-with-react 切记不要混淆 redux 中提到的 state 和 react 中的 state。Redux 状态是指应用程序的状态。一定要检查一下,如果不适合你,另一种选择是通量。


推荐阅读