title: UseState only shows on second click
body: <p>I am updating state using React useState hook on click of a form submit button, the state is not updating until second click. Believe I need to use a useEffect but not sure how to implement with an onClick.</p>
  <pre><code>const files = [
    {
      id: 'foo'
    }
  ]
  
  const [fieldName, setName] = useState({});
  
  
  const onSubmit = () =&gt; {
      files.map((file) =&gt; {
        if (!fieldName[file.id]?.value) {
          setName(() =&gt; ({
            ...fieldName,
            [file.id]: {
              value: test[file.id]?.value,
              error: true
            },
          }));
        }
      });
  
      console.log(fieldName);
      // Output = {}
      // Expected Output = { foo: { error: true } }
      
      if (fieldName) // simply to show i need access to updated fieldName at this point
  
    };
  </code></pre>