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

146
Views
Is there an easier way to reset states in React?

My React app has a form to add a new product and each product has a number of properties. When the product is added I redirect the user to the saved products page, and they can edit any saved products. I've run into some (not major) issues with the states. In some areas when editing, the state displays another products attributes because it has not been updated.

I want to clear the state so that if a product attribute's state isn't updated it is simply an empty string. Right now I'm doing this:

const resetStates = () => {
    setName("");
    setDescription("");
    setPrice(null);
}   

These are only some of the states being used, so it's cumbersome to have to reset all of them like this. Is there an easier way?

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

0

You can aggregate the state properties into a single object, like class components' state.

[formContent, setFormContent] = useState({name:'',desc:'',price: null});


const resetStates = ()=> {
    setFormContent({name: '', desc: '', price: null});
}

However the react docs seem to slightly prefer the separate state variables over a single object, since unlike this.setState updating a state variable using hooks, replaces its value.

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!