在 react-native
当我们使用redux
时我们使用的模块createStore
来自“redux”。我的问题:使用 <Provider/>
是否足够一次,这使得 Redux 商店可供我们应用程序的其余部分使用。
import ReactDOM from 'react-dom'
import { Provider } from 'react-redux'
import store from './store'
import App from './App'
const rootElement = document.getElementById('root')
ReactDOM.render(
<Provider store={store}>
<App />
</Provider>,
rootElement
)
或者我们还必须添加<Provider/>
某处?
最佳答案
使用 redux
创建商店后例如,使用 reducer ,如下所示:
const store = createStore(reducers, applyMiddleware(thunk));
那么你需要使用<Provider>
仅包装一次:
ReactDOM.render(
<Provider store={store}>
<App />
<Provider>, document.getElementById('root')
);
稍后,如果您想从商店访问任何对象,您需要使用 mapStateToProps
在你的组件中。如果需要任何修改,您需要创建操作并使用 mapDispatchToProps
调度它们.
来自documentation :
The option we recommend is to use a special React Redux component called 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.
希望这有帮助。
关于javascript - Provider使用一次就够了吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58027960/