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

361
Vistas
Zustand: change value of parameter in stored object

I am trying to create a function for a state of rated movies in Zustand.

The state consists of an array of objects, example with two entries:

  ratedMovies: [
    { imdbID: "tt0076759", userRating: "5" },
    { imdbID: "tt0080684", userRating: "10" },
  ]

Below is the function managing ratedMovies changes. Here is where the issue lies. I want it to check whether an object with the same imdbID is present in ratedMovies state. And if so to update the value of it, instead of adding another object with the same imdbID but a new value.

If I try to change the rating of one of the movies from the above state example (with them in the state ofc), I get the IF console check and the app crashes with the error: TypeError: Cannot create property 'userRating' on number '0'

If the state is empty or I change the rating of other movies, I get the ELSE console check, but they are still not added into the state.

addUserRating: (rating) => {
    console.log("rating object", rating)
    set((state) => {
      if (state.ratedMovies.find((movie) => movie.imdbID === rating.imdbID)) {
        console.log("add rating IF")
        let index = state.ratedMovies.findIndex(
          (movie) => movie.imdbID === rating.imdbID
        )
        index.userRating = rating.userRating
        return [index, ...state.ratedMovies]
      } else {
        console.log("add rating ELSE")
        return [rating, ...state.ratedMovies]
      }
    })
  }

the onChange function on the input where one can rate a movie creates an identical object as in the state array and passes it to the function managing the state of ratedMovies:

  const changeUserMovieRating = (event) => {
    const movieRating = {
      imdbID: modalDetails.imdbID,
      userRating: event.target.value,
    }
    console.log(movieRating)
    addUserRating(movieRating)
  }

Output of which is:

{imdbID: 'tt0120915', userRating: '2'}

I hope i explained everything clearly, and I will highly appreciate any tips on how to solve this issue, thanks in advance!

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

0

Sorry but this whole apprach I had at the time of asking this question had no sense and had to be reworked completely.

I decided not to add the parameter during the fetch, as in another component the same data could be fetched. So I decided to instead keep the value of the 'userRating' in the local storage and if the fetched movie was already once rated by the 'user', the value would be displayed.

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