gpt4 book ai didi

javascript - Router 和 Provider 继承

转载 作者:行者123 更新时间:2023-11-29 11:03:10 27 4
gpt4 key购买 nike

我正在学习 React 并想将 Redux 与 React Router 结合使用。Provider 是在 Router 里面还是在 Router 对面有关系吗?什么会更好:

<Provider store={store}>
<Browser>
// routes
</Browser>
</Provder>

<Browser>    
<Provider store={store}>
// routes
</Provder>
</Browser>

乍一看我可以使用这两种方式。我错过了什么吗?

最佳答案

这取决于您是否要将路由状态与您的商店同步。例如,如果您使用 react-router-redux ,你要设置,首先是Provider,然后是Router

From the docs:

import React from 'react'
import ReactDOM from 'react-dom'
import { createStore, combineReducers } from 'redux'
import { Provider } from 'react-redux'
import { Router, Route, browserHistory } from 'react-router'
import { syncHistoryWithStore, routerReducer } from 'react-router-redux'

import reducers from '<project-path>/reducers'

// Add the reducer to your store on the `routing` key
const store = createStore(
combineReducers({
...reducers,
routing: routerReducer
})
)

// Create an enhanced history that syncs navigation events with the store
const history = syncHistoryWithStore(browserHistory, store)

ReactDOM.render(
<Provider store={store}>
{ /* Tell the Router to use our enhanced history */ }
<Router history={history}>
<Route path="/" component={App}>
<Route path="foo" component={Foo}/>
<Route path="bar" component={Bar}/>
</Route>
</Router>
</Provider>,
document.getElementById('mount')
)

关于javascript - Router 和 Provider 继承,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43599532/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com