在动态路由上反应重新加载组件

时间:2017-05-14 00:36:07

标签: javascript reactjs react-router

<Route path="/user/:username/" component={UserProfile} />

我的路线设置如上,并且在

上有问题
<Link to={"/user/" + userName + "/"}>user profile</Link>

当用户转到/user/user1//user/user2/时,因为它没有重新加载组件,只是更新状态。

解决此问题的最佳方法是什么?我需要componentDidMount中的HTTP请求才能在用户名更改时执行。

1 个答案:

答案 0 :(得分:2)

路由参数更改时,路由组件不会重新加载。但它将调用componentWillReceiveProps()作为组件得到不同的道具。因此,在componentWillReceiveProps()componentDidMount内调用HTTP请求,因为React在安装过程中不会使用初始道具调用componentWillReceiveProps()

相关问题