How to capture when a <Link>
is clicked in the component? The first time you click the link, the page loads fine. But when you click the same link a second time, it doesn't reload data. This is the standard result, as it only rerenders when the state/props have changed, but I want to reload new data from the server when the page is revisited.
I've tried using componentDidUpdate
but that just ends in an infinite loop. I'm looking for a way to know when react-router-dom
has been used and query the server for fresh data.
I've listed the broken down code fragments used below.
<Switch>
<Route path="/users" component={Users}/>
</Switch>
<Link to={"/users"}>Users</Link>
class Users extends Component {
componentDidMount() {
this.getData();
console.log('componentDidMount');
}
componentDidUpdate(nextProps, nextState) {
console.log('componentDidUpdate');
}
getData() {
axios.get(this.props.table.url).then(response => {
this.setState({ users: response.data, loading: false})
})
}
render() {
return( ... )
}
}