首页 > 解决方案 > Redux 和 Context API 在同一个 React Native 项目中一起使用

问题描述

我们可以在同一个 React Native 项目中使用 Redux 和 Context API 吗?如果我们一起使用它们有什么办法或有什么问题吗?

实际上,我想使用 Context API 实现身份验证,而对于其他状态管理,我想使用 Redux Toolkit。

标签: reactjsreact-nativereact-reduxreact-hooksreact-context

解决方案


可以在项目中同时使用两者,但没有必要。你可以在 React中使用useReducer体验 redux 的简单部分。本文档对此进行了解释。它使用 Provider 来保持 contextAPI 和 redux 状态。在你的情况下,这可能就是这种情况。

<Provider store={reduxStore}>
 <Context.Provider value={contextValue}
  <App/>
 </Context.Provider>
</Provider>

推荐阅读