【发布时间】:2017-09-21 07:42:19
【问题描述】:
所以,基本上我有多个嵌套路由,例如/:platform/:account/products/:tab/:productId/summary。
一切正常到我实际的单个产品组件,但 react-router 在我的单个产品选项卡中停止正常工作。
我有一个带有标签(路线)的产品视图,当单击一个项目时,会弹出一个弹出窗口,它是单个产品视图,里面还有 3 个标签。
我的产品的子路由正在更新,但仅在下一个渲染周期。
- 我在总结中,然后单击价格:没有任何反应
(路由更改为
/:platform/:account/products/:tab/:productId/prices) - 我单击摘要:已渲染组件更改为价格
(路由更改为
/:platform/:account/products/:tab/:productId/summary) - 我单击翻译:渲染组件更改为摘要
(路由更改为
/:platform/:account/products/:tab/:productId/translations) - 我再次单击翻译:渲染组件更改为翻译 (完全没有改变路线)
我已经为此奋斗了 4 个多小时,我检查了我的树中是否有任何 PureComponent,我几乎在所有地方都使用了 withRouter,我还检查了我的产品组件是否没有更新(也许我的主视图组件阻止了更新),但一切似乎都很好,新道具是正确的。
我也尝试在connect() 中使用{ pure: false },但没有任何帮助。
为了以防万一,我还删除了这个localize HOC,但这并没有解决它。
版本
4.2.0
示例代码
这是一个从我的组件中剥离出来的渲染方法:
class ProductView extends Component {
componentWillReceiveProps() {
console.log("UPDATED PRODUCT TAB")
}
render() {
const { match, history, translate, lang, loading, data } = this.props
console.log(match)
return (
<Modal>
<TabList>
<TabItem>
<NavLink
activeClassName="active"
to={{
pathname: `/${match.params.platform}/${match.params.account}/products/${match.params.tab}/${match.params.productId}/summary`,
search: location.search,
}}
>
{translate("Summary")}
</NavLink>
</TabItem>
<TabItem>
<NavLink
activeClassName="active"
to={{
pathname: `/${match.params.platform}/${match.params.account}/products/${match.params.tab}/${match.params.productId}/prices`,
search: location.search,
}}
>
{translate("Prices")}
</NavLink>
</TabItem>
<TabItem>
<NavLink
activeClassName="active"
to={{
pathname: `/${match.params.platform}/${match.params.account}/products/${match.params.tab}/${match.params.productId}/translations`,
search: location.search,
}}
>
{translate("Translations")}
</NavLink>
</TabItem>
</TabList>
<Switch>
<Route path="/:platform/:account/products/:tab/:id/summary" component={Summary} />
<Route path="/:platform/:account/products/:tab/:id/prices" component={Prices} />
<Route path="/:platform/:account/products/:tab/:id/translations" component={Translations} />
</Switch>
</Modal>
)
}
}
const mapStateToProps = state => (...)
const mapDispatchToProps = dispatch => (...)
export default withRouter(connect(
mapStateToProps,
mapDispatchToProps,
)(localize("translate")(ProductView)))
到达产品组件的路径如下所示:
<Route exact path="/:platform/:account/products/:tab/:productId/:productTab" component={ProductView} />
添加或删除:productTab 没有区别。
在具有此产品路线的组件内,我还有其他 NavLinks 选项卡和路由器工作正常。
这里的奇怪之处在于,组件一直更新到我的产品路线,并使用正确的match 道具,但那些子路线,包括 NavLinks 仅在再次单击后更新。
【问题讨论】:
-
所以,我追踪了这个并发现它是因为 react-modal 发生的。知道那里发生了什么吗?我在他们的代码中没有看到任何
shouldComponentUpdated
标签: reactjs react-router react-router-v4 react-router-dom