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

93
Vistas
React component isn't rendering after updating state by useState

I want to update an state object userInfo which contains an array named work which is an array of objects

My Database object userInfo looks like this

{
"_id":"61a6a1d64707c03465eae052",
"fullName": "Md Nurul Islam",    
"works": 
[
    {
    "isEditing": false,
    "_id": "61a6a1d64707c03465eae053",
    "company": "Amazon Logistics",
    "position": "Sortation Associate",
    "isCurrent": true
    }, 
    {
        "isEditing": false,
        "_id":"61a6a1d64707c03465eae054",
        "company": "The Rani Indian Takeway",
        "position": "Customer Service Assistant",
        "isCurrent": false,
    }
]    
}

But I don't want update the database, i just want to update the state userInfo in frontend to go editing mood, for this when the user clicks edit next to work section, the properties isEditing will be toggled.

Below is my code

const [userInfo, setUserInfo] = useState(user);
const workToggleHandler = (work) => 
{
  setUserInfo((prev) => {
  prev.works.map((p) => {
    if (p._id === work._id) {
      p.isEditing = !p.isEditing;
    }
    return p;
  });
  return prev;
});

With this code, my state object is updated But my components are not re-rendered

about 4 years ago · Juan Pablo Isaza
3 Respuestas
Responde la pregunta

0

you can put into the useEffect when particular state is updated then that effect will call

useEffect(()=>{
  your code will go here
}[userInfo]),
about 4 years ago · Juan Pablo Isaza Denunciar

0

React does not look at the data of the object but rather on the pointer of the object. Since this is not changed (only "prev" is edited, no new object with a new object address), it doesn't not rerender. A solution would be to copy the object.

let newUserInfo = Object.assign({}, prev);
newUserInfo.works.map((p) => {
    if (p._id === work._id) {
        p.isEditing = !p.isEditing;   
    }
    return p;
});
return newUserInfo;

Disclaimer: Maybe a deep copy is necessary....

about 4 years ago · Juan Pablo Isaza Denunciar

0

I just changed my code on workToggleHandler function and its working completely as i want. Below my code

const [userInfo, setUserInfo] = useState(user);
const workToggleHandler = (work) => {
setUserInfo({
  ...userInfo,
  works: userInfo.works.map((p) => {
    if (p._id === work._id) {
      p.isEditing = !p.isEditing;
    }
    return p;
  }),
});
};
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