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

385
Vistas
Redux - Remove object from store on http response error

Consider the following flow:

I have a page with a list of "products" and a modal to create a single "product". I open the modal, fill the form and submit the form.

At this point, I dispatch an action CREATING_PRODUCT, add the product to the store and send the http request to the server.

I close the modal and display the list of results with the new product.

Let's suppose I receive an error response from the server.

Desired behavior:

I would like to display an error, remove the project from the list, re-open the modal and display the form already filled.

Question

How can I find that project and remove it the list? I don't have an id (or a combination of unique properties) to find that project in the store. I don't see a clean way to link a request/response to that "product" object in the store.

Possible solution

The client adds a "requestId" into the project before adding it to the store. On response error, I dispatch a generic "CREATE_ERROR" and I remove the project with that requestId from the store.

Extra

Same problem with edit and delete. For example during a delete should I keep a reference to the deleted project with the requestId in the store, until the http request is successful?

I bet it is a problem with a common solution, but I can't find examples. Thanks!

over 4 years ago · Santiago Trujillo
1 Respuestas
Responde la pregunta

0

In general, your Redux store should be modeled somewhat like a relational database, in that every time you have a list of data models, each element of the list should have its own identifier. This helps a lot when dealing with more complex data schemes.

You should probably store your projects as an object, something like:

{ 
  // ...other store properties
  projects: {
    "_0": { // ... project properties }
    "_1": { // ... project properties }
    // ...more projects...
  },
}

This way, whenever you need to mess with an existing project, you can just reference its id and use projects[id] to access that project. This would also solve the edit and delete cases, as you could just pass the IDs around as handles.

I like this short piece on why your Redux store should be mostly flat and why data should always have identifiers very much. It also talks about using selectors to "hide" your IDs away, which may or may not be useful for you.

In your case, as you are getting IDs from a server, you could have an ID prefix which indicates unsaved values. So your projects object would become something like:

projects: {
  "_0": { // ... }
  "_1": { // ... }
  "UNSAVED_2": { // ... }
}

This way, you could easily identify unsaved values and handle them when an error occurs, still get the benefits of generating temp IDs on the client-side in order to revert changes on error, and also warn your user if they try to leave your app while their data still hasn't been synchronized - just check if there are any "UNSAVED" IDs :)

When you get a response from the server, you could change the "UNSAVED_suffix" ID to an actual ID.

over 4 years ago · Santiago Trujillo 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