title: How to get previous state value in reactjs
body: <p>When I click <strong>New Number button</strong>, I get a random number from 0 to 15... This works fine, but now I want when I click <strong>Previous Number button</strong>, it give me the previous number/state.</p>
  
  <p>For example..</p>
  
  <p>As soon the render runs I get 12. then I click <strong>New Number button</strong> that give me 4, now I want when I click <strong>Previous Number</strong> this change the state and give me the number which was in the state before, and it was 4.
  How to achieve this functionality...</p>
  
  <p>I know <strong>componendDidUpdate</strong> have <strong>prevProp</strong> and <strong>prevState</strong> parameters but can't figure out how to call this on click.
  Here is my code</p>
  
  <pre class="lang-js prettyprint-override"><code>class App extends React.Component {
     state = {
        randomNum: ''
     }
  
     componentDidMount() {
        this.getNewRandomNum()
     }
  
     getNewRandomNum = () =&gt; {
        let randomNum = Math.floor(Math.random() * 15)
        this.setState({ randomNum })
        console.log(this.state.randomNum)
     }
  
     prevNum = () =&gt; {
  
     }
     render() {
        return (
           &lt;div&gt;
              &lt;h1&gt;{this.state.randomNum}&lt;/h1&gt;
              &lt;button onClick={this.getNewRandomNum}&gt;New Number&lt;/button&gt;
              &lt;button onClick={this.prevNum}&gt;Previous Number&lt;/button&gt;
           &lt;/div&gt;
        )
     }
  }
  </code></pre>
  
  <p>Would appreciate your help, hope I am clear.</p>