Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

103
Vistas
What is the best approach to reset state array of object in react js

I am trying to reset an array of object but I am unable to do it without rewriting the initial value of the state again. Initial state

const [state,setState]=useState([
{name:'abc',age:24},
 {name:'xyz',age:20}
])

Is there any way that I can reset the state to its initial value without the using the approach that is in the function below.

const reset =()=>{
  setState([
   {name:'abc',age:24},
   {name:'xyz',age:20}
  ])
}
about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

simply create a const and store it there

const initialState=[
   {name:'abc',age:24},
   {name:'xyz',age:20}
  ]

if you want to reset state than setState(initialState)

about 4 years ago · Juan Pablo Isaza Denunciar

0

No there is no built-in way to reset state, but you can improve your version by creating a constant initialState and not duplicating the object:

const initialState = [
{name:'abc',age:24},
 {name:'xyz',age:20}
]
...
setState(initialState);
about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda