我正在重构从React Router的V2到V4的应用程序,并且我遇到了一个问题,其中我的mapStateToProps
的属性没有传递给每个组件。这里是我的设置:OwnProps没有通过React Router V4传递
App.jsx
render() {
return (
<div>
<Header />
<Navbar />
<Routes {...this.props} />
<Footer />
</div>
)
}
const mapStateToProps = (state, ownProps) => {
const { page } = state
return {
routeProps: ownProps,
page: page
}
}
const mapDispatchToProps = (dispatch) => {
return {
pageActions: bindActionCreators(pageActionCreators, dispatch),
}
}
export default withRouter(connect(mapStateToProps, mapDispatchToProps)(App))
Routes.jsx了这一行:
<Route exact path="/:name/information" render={ ({ match }) => <InfoPage {...this.props} match={ match } /> } />
InfoPage.jsx
componentDidMount() {
const { routeProps, pageActions } = this.props
pageActions.fetchInfoPage(routeProps.match.params.name)
}
componentWillReceiveProps(nextProps) {
if (this.props.page.page !== nextProps.page.page) {
const { routeProps, pageActions } = nextProps
pageActions.fetchInfoPage(routeProps.match.params.name)
}
}
render() {
return (
<InfoPageView
page={ this.props.page }
pageActions={ this.props.pageActions }
routeProps={ this.props.routeProps }
/>
)
}
的问题是,当我尝试访问使用InfoPage的路线,即/orders/information
从不加载。当我检查Redux开发工具时,它说page
和content
未定义。在重构时我在InfoPage组件上更改的唯一东西是match.params
而不是params
。这让我相信我的Redux/Router集成有些问题,特别是ownProps/routeProps
没有正确传递。
关于这里有什么问题的任何想法?
什么的休息Routes.jsx看起来像? – Calvin
它有一个'Routes'类,它返回一个'',其中包含路由列表。前两个为: } />' –
wildlifehexagon