<Route path="/user/:username/" component={UserProfile} />
我的路线设置如上,并且在
上有问题<Link to={"/user/" + userName + "/"}>user profile</Link>
当用户转到/user/user1/
到/user/user2/
时,因为它没有重新加载组件,只是更新状态。
解决此问题的最佳方法是什么?我需要componentDidMount
中的HTTP请求才能在用户名更改时执行。
答案 0 :(得分:2)
路由参数更改时,路由组件不会重新加载。但它将调用componentWillReceiveProps()
作为组件得到不同的道具。因此,在componentWillReceiveProps()
和componentDidMount
内调用HTTP请求,因为React在安装过程中不会使用初始道具调用componentWillReceiveProps()
。