title: React setTimeout and setState inside useEffect
body: <p>I have this code and my question is why inside my answer function I get the initialState. How to set the state in a proper way to get the right one inside a callback of setTimeout function?</p>
  <pre><code>const App = () =&gt; {
    const [state, setState] = useState({
      name: &quot;&quot;,
      password: &quot;&quot;,
    });
  
    useEffect(() =&gt; {
      setState({ ...state, password: &quot;hello&quot; });
      setTimeout(answer, 1000);
    }, []);
  
    const answer = () =&gt; {
      console.log(state);
      // we get initial State
    };
    return &lt;div className=&quot;App&quot;&gt;&lt;/div&gt;;
  };
  </code></pre>