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

244
Views
error: Cannot set properties of undefined when setting the value of nested object

I want to set the value of errors to

{
  email: {
    primary:"abc@gmail.com"
  }
}

After compile, it returns an error:

Cannot set properties of undefined (setting 'primary')

App.js

import "./styles.css";
import { useState, useEffect } from "react";

export default function App() {
  const [errors, setErrors] = useState({});

  useEffect(() => {
    const newErrors = errors;
    newErrors.email.primary = "abc@gmail.com";
  }, []);
  return (
    <div className="App">
      <h1>Hello CodeSandbox</h1>
      <h2>Start editing to see some magic happen!</h2>
    </div>
  );
}

Codesandbox:
https://codesandbox.io/s/immutable-http-w0mue?file=/src/App.js

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Because newErrors.email is undefined. Just update like this:

newErrors.email = { primary: "abc@gmail.com" };
about 4 years ago · Juan Pablo Isaza Report
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!