Redux for React 中 `Provider` 和 `connect` 的区别 [英] Difference between `Provider` and `connect` in Redux for React

查看:37
本文介绍了Redux for React 中 `Provider` 和 `connect` 的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我看到使用 Redux 有两种方法可以将状态传递给 React 应用程序中的组件,一种是抛出 Provider 组件,另一种是使用连接功能.但是,我想知道为什么有两种不同的方式,哪一种在性能上比另一种更好.

I see that with Redux there's two ways of passing down the state to the components in a React app, one is throw the Provider component and the other one with the connect functionality. However, I wonder why there's two different ways and which one is better in performance than the other.

推荐答案

它们并不相互排斥.实际上,您几乎总是需要使用 Provider,即使使用 connect(参见:https://github.com/reactjs/react-redux/blob/master/docs/api.md#provider-store)

They are not mutually exclusive. Actually you will almost always need to use Provider, even with connect (See: https://github.com/reactjs/react-redux/blob/master/docs/api.md#provider-store)

如果没有 connect,您将不得不手动将存储传递给需要它们的组件,嵌套组件会变得混乱,因为您需要将其向下传递多个级别,甚至通过不需要它们的组件如果孩子需要这家商店,就去商店.

Without connect you would have to manually pass the store to components that need them, with nested components this gets messy, as you need to pass it down multiple levels, even through components that do not need the store if its children need the store.

这篇关于Redux for React 中 `Provider` 和 `connect` 的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆