React-Redux 中 <Provider> 和 connect() 之间的关系是什么?
What's the relationship between <Provider> and connect() in React-Redux?
我是 Redux 的新手,我正在尝试弄清楚 <Provider>
and connect()
.
之间的关系
据我了解,connect
将您的组件连接到商店。但是,函数参数中没有任何地方告诉 connect
那家商店的确切位置!
如果我没记错的话,<Provider>
会自动将商店提供给 connect()
。这对我来说似乎非常违反直觉,因为 Redux 的 entire point 是透明的。
所以我的问题是,<Provider>
如何在不使用某种全局变量的情况下将商店传递给 connect()
?它是否遍历整个树,搜索 connect
ed 组件然后注入自己?那不是低效的吗?如果是这样,我将如何在同一组件树中使用两个不同的商店?
其次,假设我不想使用<Provider>
,没有它我如何使用connect()
?即,我如何显式地将商店传递给每个连接的组件?
redux docs 非常棒,并且有一些关于 Provider
和 connect()
的信息
The option we recommend is to use a special React Redux component
called <Provider>
to magically make the store available to all
container components in the application without passing it explicitly.
You only need to use it once when you render the root component
本质上它利用了来自 React 的 context
的使用。根据文档,这允许您通过组件树传递数据,而不必在每个级别手动传递道具。
你没有理由不能明确地通过商店。这里的想法是它只是让事情变得更容易。
<Provider>
和 connect
是 react-redux
模块的一部分。它们一起工作,你不应该单独使用一个。您 可以 单独使用 redux
而没有 react-redux
,但您最终可能会 re-creating [=12] 的部分或全部功能=] 提供。
react-redux
通过使用 React context 来工作。上下文就像一个隐藏层,用于传递多个组件共享的变量而不显式传递它们。要使用上下文,您需要在某处设置上下文,而且,任何想要使用 context
中的内容的组件都需要获取变量。在 react-redux
中,<Provider>
本质上将存储保存到 context
中,而 connect
提供了一种从 context
.
中获取存储的方法
如果你还没有,我 recommend these videos 开始使用 Redux 和 react-redux
来自 Redux 的创建者。
我是 Redux 的新手,我正在尝试弄清楚 <Provider>
and connect()
.
据我了解,connect
将您的组件连接到商店。但是,函数参数中没有任何地方告诉 connect
那家商店的确切位置!
如果我没记错的话,<Provider>
会自动将商店提供给 connect()
。这对我来说似乎非常违反直觉,因为 Redux 的 entire point 是透明的。
所以我的问题是,<Provider>
如何在不使用某种全局变量的情况下将商店传递给 connect()
?它是否遍历整个树,搜索 connect
ed 组件然后注入自己?那不是低效的吗?如果是这样,我将如何在同一组件树中使用两个不同的商店?
其次,假设我不想使用<Provider>
,没有它我如何使用connect()
?即,我如何显式地将商店传递给每个连接的组件?
redux docs 非常棒,并且有一些关于 Provider
和 connect()
The option we recommend is to use a special React Redux component called
<Provider>
to magically make the store available to all container components in the application without passing it explicitly. You only need to use it once when you render the root component
本质上它利用了来自 React 的 context
的使用。根据文档,这允许您通过组件树传递数据,而不必在每个级别手动传递道具。
你没有理由不能明确地通过商店。这里的想法是它只是让事情变得更容易。
<Provider>
和 connect
是 react-redux
模块的一部分。它们一起工作,你不应该单独使用一个。您 可以 单独使用 redux
而没有 react-redux
,但您最终可能会 re-creating [=12] 的部分或全部功能=] 提供。
react-redux
通过使用 React context 来工作。上下文就像一个隐藏层,用于传递多个组件共享的变量而不显式传递它们。要使用上下文,您需要在某处设置上下文,而且,任何想要使用 context
中的内容的组件都需要获取变量。在 react-redux
中,<Provider>
本质上将存储保存到 context
中,而 connect
提供了一种从 context
.
如果你还没有,我 recommend these videos 开始使用 Redux 和 react-redux
来自 Redux 的创建者。