Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

176
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda