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

83
Vistas
Change in Redux state object value not re-rendering

I have an object in my store that I can being updated when actions are dispatched. But I'm not seeing the updated value for totalDevices displayed on the page. It stays with the original render. What am I doing wrong?

State

{
  deviceCount: {
    'projectId:ed4b4e40-d4e2-4540-8359-c002526f2793': {
      totalDevices: 1,
      error: {}
    },
    'projectId:a6293167-ade2-4e22-98f0-70260fcee7f7': {
      totalDevices: 0,
      error: {}
    }
  }
}

Component

  const selectDeviceCount = useSelector((state) => state.deviceCount);
  const [currDeviceCount, setCurrDeviceCount] = useState(selectDeviceCount); // Using to display value of totalDevices for each item in object

  useEffect(() => {
    loadDeviceCountByProjectID({ projectId });
  }, [loadDeviceCountByProjectID]);

  useEffect(() => {  // Should this hook trigger a rerender on currDeviceCount? 
    setCurrDeviceCount(
      selectDeviceCount
    );
  }, [selectDeviceCount]);

  return (
    <ListItem
      className={variation === 'block' ? classes.listItem : ''}
      key={projectId}
      button
      component={Link}
      to={`/${organizationName}/${name}?per_page=10&page=1`}
    >
      <ListItemText>
            {name}
          </span>
        }
      />
      <ListItemAvatar className={classes.devicesAvatar}>
        <>
          <TabletAndroidIcon className={classes.deviceIcon} />
          <Typography variant="caption">
            // Conditional rendering to prevent errors when state is undefined on first load
            {selectDeviceCount.length > 0
              ? selectDeviceCount[`projectId:${projectId}`]?.totalDevices
              : 0}
          </Typography>
        </>
      </ListItemAvatar>
    </ListItem>
  );
about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

Your use of hooks is fine, and your state should be changing. But you're measuring selectDeviceCount.length in your render, but selectDeviceCount is an object, and therefore has a length of 0. If you want to measure the number of keys inside the selectDeviceCount object, do Object.keys(selectDeviceCount).length

To mitigate selectDeviceCount being undefined, simply initialize the state to an empty object by doing useState({})

Below is the area where your code has the issue.

<Typography variant="caption">
            // Conditional rendering to prevent errors when state is undefined on first load
            {Object.keys(selectDeviceCount).length > 0
              ? selectDeviceCount[`projectId:${projectId}`]?.totalDevices
              : 0}
          </Typography>

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