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.
const files = [
{
id: 'foo'
}
]
const [fieldName, setName] = useState({});
const onSubmit = () => {
files.map((file) => {
if (!fieldName[file.id]?.value) {
setName(() => ({
...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
};