[英]React router 4 not updating when using children prop on history.push
I have this piece of code: 我有这段代码:
class Base extends Component {
changeRoute = () => {
// after this, address bar gets updated but I can't see the Users
// component, only Home component.
this.props.history.push('/users');
}
render() {
return (
<div>
<MyBar />
{this.props.children}
</div>
)
}
}
class App extends Component {
render() {
return (
<Router>
<Base>
<a onClick={this.changeRoute}>Change</a>
<Route path="/home" component={Home} />
<Route path="/users" component={Users} />
</Base>
</Router>
)
}
}
However, when I try to change location by either using history.push or push from react-router-redux I can see the browser path updated but the content is not updating, it shows the original path content. 但是,当我尝试使用history.push或从react-router-redux推送来更改位置时,我可以看到浏览器路径已更新,但内容未更新,它显示的是原始路径内容。 I also noted that whenever I refresh the browser I can see the correct content related to the path.
我还指出,每当刷新浏览器时,都可以看到与路径有关的正确内容。
When I change the code to the following, both the browser path and the content updates accordingly as expected, my question is: Why is it behaving different? 当我将代码更改为以下代码时,浏览器路径和内容都将按预期进行相应更新,我的问题是:为什么行为不同? if in my opinion both codes do the same thing.
如果我认为两个代码都做同样的事情。
class Base extends Component {
render() {
return (
<MyBar />
)
}
}
class App extends Component {
changeRoute = () => {
// after this, address bar and component are updated as expected.
this.props.history.push('/users');
}
render() {
return (
<Router>
<div>
<Base />
<a onClick={this.changeRoute}>Change</a>
<Route path="/home" component={Home} />
<Route path="/users" component={Users} />
</div>
</Router>
)
}
}
Edit: I added to the source code the push method. 编辑:我在源代码中添加了push方法。 This is the piece of code I'm using to export the App component:
这是我用来导出App组件的代码:
import {withRouter} from 'react-router';
export default withRouter(App);
This is the code I ran and it is running fine. 这是我运行的代码,运行正常。 Can you please check whether there is anything I need to change in my code to reproduce your issue?
您能否检查我的代码中是否需要更改任何内容以重现您的问题?
import React from 'react';
import ReactDOM from 'react-dom';
import {NavLink, BrowserRouter,Route} from 'react-router-dom';
class Base extends React.Component {
render() {
return (
<div>
<div>MyBar</div>
{this.props.children}
</div>
)
}
}
class App extends React.Component {
render() {
return (
<BrowserRouter>
<Base>
<NavLink to="/users">Change</NavLink>
<Route path="/home" render={()=>"Home"} />
<Route path="/users" render={()=>"Users"} />
</Base>
</BrowserRouter>
)
}
}
ReactDOM.render(<App />, document.getElementById('root'));
Edit: From what I understood, you want to be able to navigate to the user page when the change link is clicked. 编辑:据我了解,单击更改链接后,您希望能够导航到用户页面。 The correct way to do it is using the NavLink control.
正确的方法是使用NavLink控件。 This would not require the use of withRouter HOC which should only be used inside a Router component and not outside it.
这不需要使用withRouter HOC,它只能在路由器组件内部使用,而不能在路由器组件外部使用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.