我正在重構從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