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

174
Views
Default value for React state storing integer values

I have a selectedId React state that stores integer values. The state is updated with an id integer, when an item is selected from a list. Once selectedId is set, the user can click a delete button to delete the selected element, after which selectedId should be set back to its initial value again.

What is the recommended default value for selectedId? Should it set to null ( useState(null) and setSelectedId(null) ), or be undefined ( setSelectedId() )

const [selectedId, setSelectedId] = useState();
const handleSelect = id => {
  setIsAlertOpen(true);
  setSelectedId(id);
};
<Button onClick={() => {
  deleteItem(selectedId)
  setSelectedId();
}/>
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Create a constant to store the initial value, and when you delete the selectedId restore it your initial value.

const initialValue = null;
const [selectedId, setSelectedId] = useState(initialValue);

const handleSelect = id => {
  setIsAlertOpen(true);
  setSelectedId(id);
};

<Button onClick={() => {
  deleteItem(selectedId)
  setSelectedId(initialValue);
}/>
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!