gpt4 book ai didi

javascript - Redux 和 React-router 正确设置

转载 作者:行者123 更新时间:2023-12-03 05:16:48 26 4
gpt4 key购买 nike

我是 Redux 新手。尝试在当前项目中设置它。

import thunk from 'redux-thunk'
import * as reducers from './store/reducers'
import { composeWithDevTools } from 'redux-devtools-extension'

const store = createStore(combineReducers(reducers), composeWithDevTools(
applyMiddleware(thunk)
))

与提供商建立路由方案:

<Router history={browserHistory}>
<Route path='/login' component={Login} />
<Provider store={store}>
<Route path='/' component={App} onEnter={auth}>
<Route path='/token' component={Token} />
<Route path='/engagement' component={Engagement} />
<Route path='/clients' component={Clients} />
</Route>
</Provider>
</Router>

我在<Clients />中有一个基本的容器组件组件:

class Clients extends Component {
render () {
return (
<div className='any-css' >
<Row>
<ClientsSubMenuContainer someId={13} />
</Row>
</div>
)
}
}

我正在尝试与商店连接:

class ClientsSubMenuContainer extends Component {
render () {
return <ClientsSubMenu {...this.props} />
}
}
// which props do we want to inject, given the global store state?
function mapStateToProps (state) {
return {
}
}
export default connect(mapStateToProps)(ClientsSubMenuContainer)

我目前得到的只是一个警告:

Uncaught Error: Could not find "store" in either the context or props of "Connect(ClientsSubMenuContainer)". Either wrap the root component in a , or explicitly pass "store" as a prop to "Connect(ClientsSubMenuContainer)".


如何正确设置,以便所有组件(不包括或包括登录)都可以访问存储和所有 redux 功能?

更新1天后
感谢社区,我刚刚找到了问题的核心。index.js中还有一段代码这是由一名初级人员针对任何 gitFlow 无情地添加的:)我没有注意到它,因为我完全确定 master 分支不会受到任何更改。这段代码看起来像这样:

let router = (
<Router history={browserHistory} onUpdate={onUpdate}>
{Routes}
</Router>
)
render(Routes, document.getElementById('app'))

所以最终结果是:

<Router history={browserHistory} onUpdate={onUpdate}>
<Provider store={store}>
<Router history={browserHistory}>
<Route path='/login' component={Login} />
<Route path='/' component={App} onEnter={auth}>
<Route path='/token' component={Token} />
<Route path='/engagement' component={Engagement} />
<Route path='/clients' component={Clients} />
</Route>
</Router>
</Provider>
</Router>

和路由器工作得很好。唯一的提示是 redux 警告,在删除包装后消失 <Router />

最佳答案

您可以尝试用提供者包装所有组件吗?

<Provider store={store}>
<Router history={browserHistory}>
<Route path='/login' component={Login} />
<Route path='/' component={App} onEnter={auth}>
<Route path='/token' component={Token} />
<Route path='/engagement' component={Engagement} />
<Route path='/clients' component={Clients} />
</Route>
</Router>
</Provider>

关于javascript - Redux 和 React-router 正确设置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41568299/

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