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

110
Vistas
Immer not updating the property

I'm using Redux with Immer. This is my code so far:

clearMachineState(state, { payload }: ClearMachineStatePayload) {
  const childrenIds = payload.children.map((child) => child.id);
  const newMachines = typedMachineState.machine_state.machines.flatMap((machine) => {
    const tmpMachines: MachineMapping[] = [];
    childrenIds.forEach((id) => {
      console.log(id, machine.machine_id);
      if (id !== machine.machine_id) {
        const foundMachine = tmpMachines.find(
          (tmpMachine) => tmpMachine.machine_id === machine.machine_id,
        );
        if (!foundMachine) {
          tmpMachines.push(machine);
        }
      }
    });
    console.log({ tmpMachines });
    return tmpMachines;
  });
  console.log({ newMachines });
  state.machine_state.machines = newMachines;
}

I want to remove from state.machine_state.machines, these machines, which are present in payload.children. The problem is, when I use the code above, in TS, all seems well, but when I log {tmpMachines}, I see this:

Object { tmpMachines: (1) […
tmpMachines: Array [ Proxy ]
0: Proxy { <target>: null, <handler>: null }
length: 1
<prototype>: Array []
<prototype>: Object { … }

But if I use the mocked array, like so:

const newMachines = typedMachineState.machine_state.machines.flatMap((machine) => {...

I get the actual object.

But either way, the state.machine_state.machines = newMachines; assignment doesn't happen. It does, if I try to do a state.machine_state.machines = [];, but not for my created arrays.

I'd also like to map on state, not on an external source.

What am I missing?

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

0

This wasn't an immer problem. This was a logic problem. Here's the working code:

clearMachineState(state, { payload }: ClearMachineStatePayload) {
  const childrenIds = payload.children.map((child) => child.id);
  const newMachines: MachineMapping[] = [];
  typedMachineState.machine_state.machines.forEach((machine) => {
    if (!childrenIds.includes(machine.machine_id)) {
      newMachines.push(machine);
    }
  });
  state.machine_state.machines = newMachines;
},
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