RN - <Link> doesn't update location.pathname
See original GitHub issueVersion
react-router-native 4.1.1 React 15.4.1 React Native 0.42.3 Redux 3.5.2 React Redux 5.0.4
Test Case
class Content extends React.Component {
componentDidMount() {
this.props.history.listen((location, action) => {
console.log(location, action);
console.log(this.props.location);
});
this.props.history.push('/foo');
}
render() {
let Foo = <Text>{'Foo'}</Text>;
let Bar = <Text>{'Bar'}</Text>;
return (
<View>
<Link to="/foo"><Text>{'Foo'}</Text></Link>
<Link to="/bar"><Text>{'Bar'}</Text></Link>
<Switch>
<Route path="/foo" component={Foo} />
<Route path="/bar" component={Bar} />
</Switch>
</View>
);
}
}
export default withRouter(connect()(Content))
// ------------------------------------------------------
class App extends React.Component {
// ... init store
render() {
<Provider store={this.store}>
<NativeRouter>
<Content/>
</NativeRouter>
</Provider>
}
}
Steps to reproduce
Click a link or call this.props.history.push('/foo')
Expected Behavior
Replacement of this.props.location.pathname and an updated component
Actual Behavior
this.props.location.pathname is not updated, and the component is not rerendered either. this.props.history is updated as can be seen in this.props.history.listen. Setting this.props.location.pathname manually does result in the expected behavior.
Issue Analytics
- State:
- Created 6 years ago
- Reactions:1
- Comments:8 (2 by maintainers)
Top Results From Across the Web
RN - <Link> doesn't update location.pathname · Issue #5251
location.pathname is not updated, and the component is not rerendered either. this.props.history is updated as can be seen in this ...
Read more >document.location.pathname updating when routing from ...
document.pathname does not update which results in respective menu item not being highlighted and last one remain highlighed. But the routing ...
Read more >Link v6.6.1 - React Router
In react-router-dom , a <Link> renders an accessible <a> element with a real href that points to the resource it's linking to. This...
Read more >Linking - React Native
Linking gives you a general interface to interact with both incoming and outgoing app links.
Read more >location.pathname - Web APIs - MDN Web Docs
The pathname property of the Location interface is a string containing the path of the URL for the location. If there is no...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found

I failed to get it working after hours of looking into my code, documentation and examples. Not expecting to get it to work I now work around this by doing this in
Content’scomponentWillMount():Well, isn’t that like a core functionality of the router to change routes? 😆 I just encountered the same problem. I tried debugging and it seems that
NativeRouteris not even listening to any changes. There is a single listener inRouterand strangely enough, itscomputeMatchmethod always setspathnameto/https://github.com/ReactTraining/react-router/blob/c7993ce58ee1e4c1176b576a46bcc5561ac751ad/packages/react-router/modules/Router.js#L60
I am failing to understand how is this supposed to work since
NativeRouteris just aMemoryRouterand there are no listeners whatsoever.@timdorr Since you have closed this issue, do you have some idea what might be wrong here?
Edit: Well, it looks like it must be something strange on my side. I copied simple example to Snack Expo and it’s working there just fine… https://snack.expo.io/BJiha0g5z You might want to use your real phone instead of waiting in appetize queue