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

99
Views
Cómo configurar o actualizar el estado de Array en el componente secundario

Estoy tratando de establecer el estado de la matriz desde el componente secundario que se encuentra en el componente principal.

Traté de usar setNumberOfVar([...varsLines]); este método, pero no está actualizando el estado inmediatamente. Se actualiza pero un paso atrás.

Mi código

 const removeVar = (e, val) => { e.preventDefault(); var varsLines = numberOfVar; varsLines = numberOfVar.filter((item) => { return item.postion != val; }); varsLines = varsLines.map((item) => { return { ...item, postion: item.postion > val ? item.postion - 1 : item.postion, }; }); console.log(varsLines); setNumberOfVar([...varsLines]); // <== this line is not updating the state immediately; console.log(numberOfVar); };
over 4 years ago · Santiago Trujillo
2 answers
Answer question

0

intenta hacer algo como esto

 setNumberOfVar(prevState=>prevState.filter().map())

use sus propias condiciones en el filtro y el mapa y vea si funciona o no.

over 4 years ago · Santiago Trujillo Report

0

setNumberOfVar() es una operación asíncrona y no actualizará el estado inmediatamente.

Si desea obtener algo como una devolución de llamada cuando se actualiza el estado, puede usar el enlace useEffect que se ejecuta en la actualización del estado.

 import {useEffect } from 'react'; useEffect(() => { console.log(numberOfVar); },[numberOfVar]);
over 4 years ago · Santiago Trujillo 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!