简体   繁体   English

redux、react-redux、redux-thunk 之间有什么区别?

[英]What are differences between redux, react-redux, redux-thunk?

I am using React + Flux.我正在使用 React + Flux。 Our team is planning to move from flux to redux.我们的团队正计划从flux 转向redux。 Redux is very confusing for me coming from flux world. Redux 对来自 Flux 世界的我来说非常困惑。 In flux control flow is simple from Components -> actions -> Store and store updates back components . Influx 控制流很简单,从Components -> actions -> Store and store updates back components Its simple and very clear.它的简单和非常清楚。

But in redux its confusing.但在 redux 中它令人困惑。 There is no store here, yes there are some examples without using store.这里没有商店,是的,有一些不使用商店的例子。 I went through several tutorials, it seems everyone has their own style of implementation.看了几个教程,好像每个人都有自己的实现风格。 Some are using Containers and some are not.有些正在使用容器,有些则没有。 (I don't know this Containers concept and not able to understand what mapStateToProps, mapDispatchToProps does). (我不知道这个 Containers 概念,也无法理解 mapStateToProps、mapDispatchToProps 的作用)。

  1. Can someone clearly explain how control flow happens in redux ?有人可以清楚地解释控制流是如何在 redux 中发生的吗?
  2. What are roles of components/containers/actions/action creators/store in redux ?组件/容器/动作/动作创建者/商店在 redux 中的作用是什么?
  3. Difference between redux/react-redux/redux-thunk/any others ?? redux/react-redux/redux-thunk/其他的区别??
  4. It would be very helpful if you can post links to any simple and precise redux tutorials.如果您可以发布指向任何简单而精确的redux 教程的链接,那将非常有帮助。
  1. Can someone clearly explain how control flow happens in redux ?有人可以清楚地解释控制流是如何在 redux 中发生的吗?

Redux has (always) a single store. Redux(总是)有一个存储。

  1. Whenever you want to replace the state in the store, you dispatch an action.每当您想替换 store 中的状态时,您就可以调度一个动作。

  2. The action is caught by one or more reducers.该动作被一个或多个减速器捕获。

  3. The reducer/s create a new state that combines the old state, and the dispatched action. reducer/s 创建一个新状态,它结合了旧状态和分派的动作。

  4. The store subscribers are notified that there is a new state.商店订阅者被通知有一个新的状态。

  1. What are roles of components/containers/actions/action creators/store in redux ?组件/容器/动作/动作创建者/商店在 redux 中的作用是什么?
  • Store - holds the state, and when a new action arrives runs the dispatch -> middleware -> reducers pipeline, and notifies subscribers when the state is replaced by a new one.存储 - 保存状态,当新操作到达时运行调度 -> 中间件 -> 减速器管道,并在状态被新状态替换时通知订阅者。

  • Components - dumb view parts which are not aware of the state directly.组件 - 不直接知道状态的哑视图部件。 Also known as presentational components.也称为展示组件。

  • Containers - pieces of the view that are aware of the state using react-redux.容器 - 使用 react-redux 了解状态的视图片段。 Also known as smart components, and higher order components也称为智能组件和高阶组件


Note that containers / smart components vs. dumb components is just a good way to structure your app.请注意,容器/智能组件与哑组件只是构建应用程序的好方法。


  • Actions - same as flux - command pattern with type and payload.动作 - 与通量相同 - 具有类型和有效载荷的命令模式。

  • Action creators - DRY way of creating actions (not strictly necessary)动作创建者 - 创建动作的 DRY 方式(并非绝对必要)

  1. Difference between redux/react-redux/redux-thunk/any others ? redux/react-redux/redux-thunk/其他之间的区别?
  • redux - flux like flow with a single store, that can be used in whatever environment you like including vanilla js, react, angular 1/2, etc... redux - 单一商店的流量,可以在您喜欢的任何环境中使用,包括 vanilla js、react、angular 1/2 等...

  • react-redux - bindings between redux and react. react-redux - redux 和 react 之间的绑定。 The library offers a set of react hooks - useSelector() , and useStore() to get the data from the store, and useDispatch() to dispatch actions.该库提供了一组反应钩子- useSelector()useStore()以从存储中获取数据,以及useDispatch()以调度操作。 You can also use the connect() function to create HoCs (higher order components), that listen to the store's state changes, prepare the props for the wrapped component, and re-render the wrapped components when the state changes.您还可以使用connect()函数来创建 HoC(高阶组件),用于监听 store 的状态变化,为包装的组件准备 props,并在状态更改时重新渲染包装的组件。

  • redux-thunk - middleware that allows you to write action creators that return a function instead of an action. redux-thunk - 允许您编写返回函数而不是动作的动作创建器的中间件。 The thunk can be used to delay the dispatch of an action, or to dispatch only if a certain condition is met. thunk 可用于延迟动作的调度,或仅在满足特定条件时才调度。 Used mainly for async calls to api, that dispatch another action on success / failure.主要用于异步调用 api,在成功/失败时调度另一个操作。

  1. It would be very helpful if you can post links to any simple and precise redux tutorials.如果您可以发布指向任何简单而精确的 redux 教程的链接,那将非常有帮助。

To answer you title question:回答你的标题问题:

What are differences between redux, react-redux, redux-thunk? redux、react-redux、redux-thunk 之间有什么区别?

  1. redux: main library (independent from React) redux:主库(独立于 React)
  2. redux-thunk: a redux middleware which helps you with async actions redux-thunk:一个 redux 中间件,可帮助您处理异步操作
  3. react-redux: connects your redux store with ReactComponents react-redux:将您的 redux 存储与 ReactComponents 连接起来
  • redux: Library for managing application state. redux:管理应用程序状态的库。
  • react-redux: Library for managing React application (redux) state. react-redux:用于管理React应用程序 (redux) 状态的库。
  • redux-thunk: a middleware for logging, crash reporting, talking to an async API, routing etc... redux-thunk:用于日志记录、崩溃报告、与异步 API 通信、路由等的中间件...

bellow image demonstrates how data flow in redux : how the data flows through Redux?下图演示了数据如何在 redux 中流动:数据如何在 Redux 中流动? Advantages of Redux are listed below: Redux 的优点如下:

Predictability of outcome – Since there is always one source of truth, ie the store, there is no confusion about how to sync the current state with actions and other parts of the application.结果的可预测性——因为总是有一个事实来源,即商店,关于如何将当前状态与应用程序的动作和其他部分同步,没有任何混淆。 Maintainability – The code becomes easier to maintain with a predictable outcome and strict structure.可维护性——代码变得更容易维护,具有可预测的结果和严格的结构。 Server-side rendering – You just need to pass the store created on the server, to the client-side.服务器端渲染——您只需要将在服务器上创建的商店传递到客户端。 This is very useful for initial render and provides a better user experience as it optimizes the application performance.这对于初始渲染非常有用,并提供更好的用户体验,因为它优化了应用程序性能。 Developer tools – From actions to state changes, developers can track everything going on in the application in real-time.开发人员工具——从操作到状态更改,开发人员可以实时跟踪应用程序中发生的一切。 Community and ecosystem – Redux has a huge community behind it which makes it even more captivating to use.社区和生态系统——Redux 背后有一个庞大的社区,这使得它使用起来更加迷人。 A large community of talented individuals contribute to the betterment of the library and develop various applications with it.一个庞大的人才社区为图书馆的改进做出了贡献,并用它开发了各种应用程序。 Ease of testing – Redux's code is mostly functions which are small, pure and isolated.易于测试——Redux 的代码主要是小、纯和隔离的函数。 This makes the code testable and independent.这使得代码可测试且独立。 [Organization][2] – Redux is precise about how code should be organized, this makes the code more consistent and easier when a team works with it. [组织][2] – Redux 对代码的组织方式非常精确,这使得代码在团队使用时更加一致和容易。

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

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