I'm trying to run a function every time the url is changed. Basically I have a navbar which contains a search form. When the user searches it will update the url to reflect the data entered by the user:
this.props.history.push(`/searchusers?q=${this.state.searchBarData}`)
This will push the user to the /searchusers
page
On the /searchusers
page I would like to add a listener to check when the url of the page is updated (i.e. if the user enters more data into the search bar). I am using the following code on th /searchusers
page to initialize the listener:
componentDidMount(){
window.addEventListener('hashchange', console.log("changed"), false)
}
The function runs when the component loads initially, but does not work when the url is changed after that. Any ideas where I might be going wrong? Thanks!