title: React Unhandled Rejection (TypeError): Cannot read property 'state' of undefined
body: <p>After I create a new game via a form I can redirect to newly created game's show page but then if I want to go to the games index page it reloads the newly created game's show page again. I need to refresh to go to the game's index page. </p>
  
  <p>So I created <code>clearSubmit()</code> method, changed <code>hasSubmitted</code> value to false again, passed the method into the show page's route and in show page's <code>component(GameShow.js)</code> I called clearSubmit() method as props. </p>
  
  <p>Right now when I submit the form to create a game I got this error:</p>
  
  <pre><code>React Unhandled Rejection (TypeError): Cannot read property 'state' of 
  undefined
  
  src/components/GameShow.js:14
  
  11 | constructor(props) {
  12 |   super(props)
  13 |   this.state = {
  &gt; 14 |     game: this.props.location.state.selectedGame
  15 |   }
  16 | }
  </code></pre>
  
  <p><strong>App.js</strong></p>
  
  <pre><code> class App extends Component {
  
    constructor(props) {
    super(props)
    this.state = {
     games: [] ,
     name: '',
     img_url: '',
     genre: '',
     platforms: '',
     video_url: '',
     description: '',
     hasSubmitted: false,
     newGame: {}
   }
  
     this.handleSubmit = this.handleSubmit.bind(this)
     this.handleChange = this.handleChange.bind(this)
     this.clearSubmit = this.clearSubmit.bind(this) 
  }
  
  
     handleChange(e){
      this.setState({
        [e.target.name]: e.target.value
      })
    }
  
  
     handleSubmit = (e) =&gt; {
      e.preventDefault();
      const { name, img_url, genre, platforms, video_url, description} = 
             this.state;
  
  
     axios.post('http://localhost:4000/api/games', { name: name, img_url: 
      img_url, genre: genre, platforms: platforms, video_url: video_url, 
      description: description })
       .then((result) =&gt; {
         this.setState({
           hasSubmitted: true,
           newGame: result.data
         })
      });
    }
  
    clearSubmit() {
      this.setState({
        hasSubmitted: false
      })
    }
  
    render() {
     return (
      &lt;Router&gt;
       &lt;div&gt;
        &lt;main&gt;
          &lt;Route exact path='/games' render={() =&gt; {
            let newGame = this.state.newGame
            return this.state.hasSubmitted
              ? &lt;Redirect to=
                     {{pathname:`/games/${this.state.newGame.name}`, 
                       state: {selectedGame: newGame}}} 
                /&gt;
              : &lt;GameForm handleChange={this.handleChange}   
                handleSubmit={this.handleSubmit} games={this.state.games} 
                /&gt;
            } }
          /&gt;
  
          &lt;Route exact path='/games' render={() =&gt; {
              return (
              &lt;GameIndex handleChange={this.handleChange}   
                handleSubmit={this.handleSubmit} games={this.state.games} 
              /&gt;
            )
            } }
          /&gt;
          &lt;Route path="/games/:name" render={() =&gt; {
              return (
                &lt;GameShow clearSubmit={this.clearSubmit} /&gt;
              )      
            } }
       /&gt;
        &lt;/main&gt;
      &lt;/div&gt;
  </code></pre>
  
  <p><strong>GameIndex.js</strong></p>
  
  <pre><code>class GameIndex extends Component {
  
  
      render() {
         let videoGames = this.props.games.map((game, i) =&gt;{
         let pathname = `/games/${game.name}`
  
         return (
          &lt;div key={i}&gt;
            &lt;p&gt;&lt;Link to=
           {{ pathname, state: {selectedGame: game}}}&gt; {game.name}&lt;/Link&gt;&lt;/p&gt;
          &lt;/div&gt;
      )
    })
  </code></pre>
  
  <p><strong>GameShow.js</strong></p>
  
  <pre><code> class GameShow extends Component {
    componentDidMount() {
     this.props.clearSubmit()
   }
  
   constructor(props) {
    super(props)
    this.state = {
     game: this.props.location.state.selectedGame
   }
  }
  </code></pre>
  
  <p>I couldn't figure out what am I missing. </p>
  
  <p>Thanks.</p>