Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

175
Views
React state updates when using object, but not when using array

I have a question about some weird behavior when using React state.

If I change the default type of the groupedFormErrors variable in the helper function to [] instead of {} the state in the page element formErrors doesn't update. Would be great if somebody could explain why this happens.

Page element

const [formErrors, setFormErrors] = useState();

const signUpHandler = async (e) => {
    e.preventDefault();
    const response = await submitForm();
    if (response.status === 422) {
        setFormErrors(groupFormErrors(response));
    }
};

Helper function

export default function GroupFormErrors(response) {
    let groupedFormErrors = {}; // <<<<<<<<<<<<<<<<<<<<< This here
    if (response.status === 422) {
        response.data.forEach((error) => {
            if (groupedFormErrors.hasOwnProperty(error.param)) {
                groupedFormErrors[error.param].push(error.msg);
            } else {
                groupedFormErrors[error.param] = [];
                groupedFormErrors[error.param].push(error.msg);
            }
        });
    }
    return groupedFormErrors;
}
about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!