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

137
Views
Using Multiple useState hooks

In React Native project to get input data in form I have used multiple hooks. Is there any better or efficient way to do so?

thanks

const [name, setName] = useState('');
const [surname, setSurname] = useState('');
const [rollNo, setRollNo] = useState('');
const [phone, setPhone] = useState('');
const [email, setEmail] = useState('');
const [address, setAddress] = useState('');
about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

I think this is the correct way to do it. Simple states are a good way to use them. I believe it's better practice to not use '' as initial state but null. So that there is no confusion possible.

about 4 years ago · Juan Pablo Isaza Report

0

You can use useReducer hook or you can also use custom useSetState which which works similar to how this.setState works in class components. It merges object changes into current state.

If the data received from the back-end API is an object containing the user details such as name and email etc etc... then you should not have separate state variables for each and every property. Instead you should save the entire user object in a single state variable. If you need you can de-structure it later like so:

const User = ({ props }) => {
  // good
  const [user, setUser] = useState({});

  const { name, email } = user;

  //bad
  const [name, setName] = useState("");
  const [email, setEmail] = useState("");
};
about 4 years ago · Juan Pablo Isaza Report

0

This may help you. Better use useReducer

import { useReducer } from "react";

const studentInitState = {
  name: null,
  surname: null,
  rollNo: null,
  phone: null,
  email: null
};

function Test(props) {
  const [student, setStudent] = useReducer(
    //--> below line implies. that take old state & update the current state and return the `new state`  this is called a `Reducer` function.
    (student, newDetails) => ({ ...student, ...newDetails }),

    studentInitState
  );


  return (
    <div>
      <button onClick={() => setStudent({ name: "Sachin" })}>Update</button>
      {student.name}
    </div>
  );
}

export default Test;

    
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!