title: is it good in react to put a variable as a state value and update it directly
body: <p>now I'm working with react form validation logic</p>
  <p>I saw some tutorials on the internet about validating a form in react</p>
  <p>and they made a normal variable and put it as a value to a state and update it directly without calling the &quot;set&quot; function provided by react like that</p>
  <pre><code>let errorsObject = {email: &quot;&quot;, password: &quot;&quot;};
  const [errors, setErrors] = useState(errorsObject)
  </code></pre>
  <p>and they update the variable directly like that</p>
  <pre><code>const onSubmit = (e) =&gt; {
     e.preventDefault();
     if (emailValue === &quot;&quot;) {
        errorsObject.email = &quot;please fill in this field&quot;
     }
  }
  </code></pre>
  <p>now I think this is wrong to update the state value like that I know the the best way to update a state is like that</p>
  <pre><code>if (emailValue === &quot;&quot;) {
        setError({email: &quot;please fill in this field&quot;})
  }
  </code></pre>
  <p>but there is a problem here</p>
  <p>if I updated the state like this it overrides the state last data</p>
  <p>and If I tried to get the latest state data like this</p>
  <pre><code>setErrors((prevState) =&gt; {
     return {...prevState, email: &quot;please fill in this field&quot;}
  })
  </code></pre>
  <p>there is another error</p>
  <p>If I submitted the form again and eddited email input data, the last validation data will be stored from the last check!</p>
  <p>I hope you understand this, and If there another way to validate a form please tell me</p>
  <p>and Thank you all</p>