0

I have this code here:

const favouriteMovies = (state = initialState, action) => {
  switch(action.type) {
    case 'setMovieToFavourites': return {
      ...state,
      hearted: [...state.hearted, action.movie]
    }
    default: return state;
  }
}

const unlikeMovie = (state = initialState, action) => {
  switch(action.type) {
    case 'unlikeMovie': return {
      ...state,
      hearted: state.hearted.filter(item => item !== action.movie),
    }
    default: return state;
  }
}

So the first function favouriteMovies adds movies to array and with the second function I want to remove the movie from the hearted movies. In the unlikeMovie function I get the movieID which equals to the one of the liked movies but it won't remove the movie from the hearted state. Why it isn't removing ?

7
  • You compare movie objects directly, not their IDs. If object reference changes over time with other updates, that may be the cause of the problem. Commented Nov 10, 2019 at 22:17
  • But the movie only have value of movie id. Commented Nov 10, 2019 at 22:18
  • Have you tried to see what state.hearted.filter(item => item !== action.movie) returns? Maybe move the code and assign the result to a variable to make sure it's doing what you expect. Also, any chance that unlikeMovie isn't the action type that's being created? Commented Nov 10, 2019 at 22:19
  • Can you please show some more code? If your action.movie is the id then the returned state should be filtered Commented Nov 10, 2019 at 22:24
  • 1
    Are you sure you're calling the dispatch for unlikeMovie ? Commented Nov 10, 2019 at 22:35

2 Answers 2

2

Looks like you might need to compare the IDs to filter out the movie in the unlikeMovie function.

Hard to tell for sure without knowing more about the structure of the movie object but assuming action.movie is the same type of object as item in your code sample and they both share an ID property of movieID, something like this should work:

const unlikeMovie = (state = initialState, action) => {
  switch(action.type) {
    case 'unlikeMovie': return {
      ...state,
      hearted: state.hearted.filter(item => item.movieID !== action.movie.movieID),
    }
    default: return state;
  }
}

Hope this helps!

Sign up to request clarification or add additional context in comments.

9 Comments

movie only have value of movie id. Only numbers
I see, thanks. Can you confirm with a console.log or debugger statement that action.movie has the value of the ID you're expecting? Just wondering if it has something to do with how the action is being dispatched.
Console logger: setMovieToFavourites", movie: 475557 unlikeMovie", movie: 475557
Can you log the hearted array before the filter?
favouriteMovies: hearted: (3) [475557, 290859, 420818]
|
1

Can you check this code ?

const movies = (state = initialState, action) => {
  switch(action.type) {
    case 'setMovieToFavourites': return {
      ...state,
      hearted: [...state.hearted, action.movie]
    }

    case 'unlikeMovie': return {
      ...state,
      hearted: state.hearted.filter(item => item !== action.movie),
    }
    default: return state;
  }
}

export default movies;

4 Comments

Wooorks! Thanks!!
:) Happy for you !
It;s because there was two seperate functions with initalState ?
All your requests to unlike a movie are captured with first reducer. the name of it doesn't matter (favouriteMovies in your case, movies in my case) because redux search for an action type that match ( unlikeMovie in this case). so your requests stop at default: return state; of first reducer. Hope it's clear :). upvote if answer and comment help!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.