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

105
Visualizações
How to remove element(s) from a state array variable?

I have a state variable tasks, which is an array of objects:

const [tasks, setTasks] = useState([
    {
      user: "",
      text: "Finish this page",
      deadline: new Date("05/01/2022"),
      complete: true,
      priority: "high",
      project: "Gravity",
      // ToDo: Set priority based on date while creating task
    },
    {
      text: "Finish Pierian UI/UX",
      deadline: new Date("05/10/2022"),
      complete: false,
      priority: "med",
    },
    {
      text: "Finish Internship",
      deadline: new Date("05/05/2022"),
      complete: false,
      priority: "low",
    },
    {
      text: "Anaadyanta",
      deadline: new Date("05/12/2022"),
      complete: false,
      priority: "med",
    },
    {
      text: "Random task",
      deadline: new Date("05/19/2022"),
      complete: false,
      priority: "high",
    },
  ]);

I want to remove elements whose complete is true. I am unable to think of the correct syntax while using setTasks. Please help.

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

You should filter tasks and set new state:

const updatedTasks = tasks.filter((el)=> el.complete !== 'true');
setTasks(updatedTasks);
about 4 years ago · Juan Pablo Isaza Relatório

0

you should pass callback inside setTasks:

setTask((prevTasks) => prevTasks.filter(({ complete }) => !complete));

The prevTasks always going to be the latest state, React insures it. You should always use callback when next state relies on previous. Also the name of prev variable passed in callback could be whatever you choose, here I named it prevTasks.

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