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

98
Vistas
React - Handle errors in reducers

Note: I am not using Redux (instead, React Context + reducers)

I have the following reducer:

export default (contents, action) => {
  switch (action.type) {
    case "like-content": {
      let { content } = action;

      const prevContent = contents.get(content.id);

      return new Map([...contents, [content, {
           ...content,
           totalLikes: prevContent.totalLikes + 1 
      }]);
    }

   ...

And I need to check if the given content is stored in my contents map before updating the state.

I have think about updating my code to:

export default (contents, action) => {
  switch (action.type) {
    case "like-content": {
      let { content } = action;

      const prevContent = contents.get(content.id);

      if (!prevContent) throw Error("The content doesn't exist");

      return new Map([...contents, [content, {
           ...content,
           totalLikes: prevContent.totalLikes + 1 
      }]);
    }

 ...

But, personally, it seems incorrect, as I am "breaking" the state update.

How can I handle this?

about 4 years ago · Juan Pablo Isaza
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