19 November 2019

Use this snippet when you have created a menu using BrowserRouter from 'react-router-dom' and you need to reset the state when you click on the current menu item (same url path as you are currently on).

Source code viewer
  1. componentWillReceiveProps(nextProps) {
  2. if (nextProps.location.pathname === window.location.pathname) {
  3. this.setState({...this.initialState})
  4. }
  5. }
Programming Language: ECMAScript