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

138
Views
Any method to use single useState()

What I wanted to know ,that is there any method / technique to replace many useState() hook in one hook.

Like,

const [firstName, setFirstName] = useState('');
const [email, setEmail] = useState('');
const [age, setAge] = useState('');
const [people, setPeople] = useState([]);

Instead of writing the whole thing again and again , we define it any one of them.

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

0

You can use the useReducer hook instead of useState.

This is their official docs on useReducer

about 4 years ago · Juan Pablo Isaza Report

0

You can create a state object and use it.

Note: When you set state, make sure you merge with the previous state. don't just call setState("abc");

const [state, setState] = useState({ firstName: "", email: "" });

// somewhere in your code, This is important
setState((pre) => ({ ...pre, firstName: "John", email: "ab@gm.co" }));
about 4 years ago · Juan Pablo Isaza Report

0

NO there isn't any way to define all that state using a single useState. unless you want to manage your state as a single object which you can define like this

const [state, setState] = useState({
    // key: value
});

There are advantage in keeping each state separate from other state. or you can simple use a class component instead of functional component where you can have all your state manage inside of a single object

By the way this bring some kind of head-hake for each state property update you have to rely on spread operator to avoid to replace the entire object with a single value instead of an object with properties

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!