title: Waiting for react state to update in function component
body: <p>How can I make sure that <code>handleOpen</code> only executes after all the states (<code>nameError</code>, <code>emailError</code>, <code>messageError</code>) have been updated? My problem is that since state doesn't update immediately, sometimes <code>handleOpen</code> executes when it shouldn't.</p>
  <pre><code>const handleSend = (e) =&gt; {
  e.preventDefault();
  if (name === &quot;&quot;) {
    setNameError(true);
  }
  if (email === &quot;&quot;) {
    setEmailError(true);
    setEmailErrorMessage(&quot;Please type your email!&quot;);
  }
  if (!email.includes(&quot;@&quot;)) {
    setEmailError(true);
    setEmailErrorMessage(&quot;Invalid email address&quot;);
  }
  if (message === &quot;&quot;) {
    setMessageError(true);
  }
  if (!nameError &amp;&amp; !emailError &amp;&amp; !messageError) {
    handleOpen();
  }
  </code></pre>
  <p>};</p>