Title: How To Get State of Switch If No Match in React ReactRouter
Tags: reactjs;react-router
Question: I'm using the ReactRouter and want to do something like set state when the RouteNotFound component is hit (last one means no others above matched).

my ```this.handler``` does a setState so that I can tell that it was called. That of course gives me an error saying "Cannot update during an existing state transition such as in a render".

Is there a way I can set my state to tell me which (I really want to know the last one) of the switch statements go executed?

```render() {
    return (
        <div&gt;
            <Switch&gt;
                <Route exact path="/" component={Home}/&gt;
                <Route exact path="/p1" component={p1}/&gt;
                <Route exact path="/p2" component={p2}/&gt;
                <RouteNotFound action={this.handler} &gt;</RouteNotFound&gt;
            </Switch&gt;
        </div&gt;
    );
}
```

Comment: What you are looking for is the onEnter functionality of react-router which in react-router-v4 can be acieved through lifecycle methods. Check the duplicate question for implementation

Here is the accepted answer: Here we go:

not-found.js:

```import React from 'react';
import { Route } from 'react-router-dom';

const RouteNoteFound = ({ action }) =&gt; (
  <Route
    render={props =&gt; (
      <NotFoundPageComponent action={action} /&gt;
    )}
  /&gt;
);


export default RouteNoteFound;

class NotFoundPageComponent extends React.Component {
  constructor(props) {
    super(props);
  }

  componentWillMount() {
    this.props.action()
  }

  render() {
    return (
      <div&gt;404 not found</div&gt;
    )
  }
}
```

and in index.js:

```handler = () =&gt; {
    alert("alert from app.js but run in not found page")
}

render() {
    return (
      <BrowserRouter&gt;
        <Switch&gt;
          <Route exact path="/" component={Page} /&gt;
          <Route path="/page" component={Page} /&gt;
          <RouteNoteFound action={this.handler} /&gt;
        </Switch&gt;
      </BrowserRouter&gt;
    );
}
```



here is DEMO in stackblitz.

Comment for this answer: in index.js, I want to push that "state" up to its parent and then again up to its parent.  I think I want to set state in the action handler and then in ComponentDidUpdate, send the state up to parent. When I do that I get into recursion problem.

Here is another answer: Try this.

Define the not found route like this:

```<Route render={props =&gt; <RouteNotFound action={this.handler} /&gt;}&gt;</Route&gt;
```

Because, as per the Doc: 

```
  A ```<Route&gt;``` with no path prop or a ```<Redirect&gt;``` with no from prop will
  always match the current location.
```

Check the Doc example for "No Path Match".

Now use ```componentDidMount``` lifecycle method in RouteNotFound component, whenever RouteNotFound will get rendered, ```componentDidMount``` will be called. Call the parent function from ```componentDidMount```, like this:

```componentDidMount(){
   this.props.action();
}
```

Now do setState inside ```action``` in parent component.

Comment for this answer: not sure, why it is throwing error, are you using `()` with `this.handler` ? because it should work. It is working when doing setState inside `componentWillMount`?
Comment for this answer: The problem still exists in {this.handler}. I gather I can not do a state update inside the render function. Warning: Cannot update during an existing state transition (such as within `render` or another component's constructor). Render methods should be a pure function of props and state; constructor side-effects are an anti-pattern, but can be moved to `componentWillMount`.
Comment for this answer: what are you expection is in the {this.handler}. I've got the setState(...) in that which is causing the error I listed in the previous comment

{'source': 'StackExchangeDataset', 'subset': 'stackoverflow.com', 'metadata': {'AnswerCount': 2, 'CommentCount': 1, 'ContentLicense': 'CC BY-SA 3.0', 'CreationDate': '2018:00:01/03/18', 'FavoriteCount': None, 'PostTypeId': 1, 'Tags': '<reactjs><react-router>', 'Title': 'How To Get State of Switch If No Match in React ReactRouter', 'ViewCount': 884, 'id': 48070856, 'user_AboutMe': None, 'user_AccountId': 12504256.0, 'user_DisplayName': 'Pete', 'user_Reputation': 2679.0, 'user_Views': 212.0, 'user_id': 9101619.0}}