FAQ: Refactoring with Redux Toolkit - Review

This community-built FAQ covers the “Review” exercise from the lesson “Refactoring with Redux Toolkit”.

Paths and Courses
This exercise can be found in the following Codecademy content:

Learn Redux

FAQs on the exercise Review

There are currently no frequently asked questions associated with this exercise – that’s where you come in! You can contribute to this section by offering your own questions, answers, or clarifications on this exercise. Ask or answer a question by clicking reply (reply) below.

If you’ve had an “aha” moment about the concepts, formatting, syntax, or anything else with this exercise, consider sharing those insights! Teaching others and answering their questions is one of the best ways to learn and stay sharp.

Join the Discussion. Help a fellow learner on their journey.

Ask or answer a question about this exercise by clicking reply (reply) below!
You can also find further discussion and get answers to your questions over in Language Help.

Agree with a comment or answer? Like (like) to up-vote the contribution!

Need broader help or resources? Head to Language Help and Tips and Resources. If you are wanting feedback or inspiration for a project, check out Projects.

Looking for motivation to keep learning? Join our wider discussions in Community

Learn more about how to use this guide.

Found a bug? Report it online, or post in Bug Reporting

Have a question about your account or billing? Reach out to our customer support team!

None of the above? Find out where to ask other questions here!

I wanted to tackle the challenges at the end of the Redux Toolkit lesson:

If you want to experiment further with the knowledge you’ve learned thus far, check out the challenges below!

  • Update the application to include a user profile slice to keep track of the favorite recipes.
  • Add a new case reducer to handle adding user-created recipes.
  • Add a rating system to recipes so the user can “rank” their favorite recipes.

I’m not sure how to start. Is there a walkthrough available for these?

I was wondering how to download all the files from this lesson? I would like to continue working on the challenges at the bottom without having to come back here in the Syllabus each time.

In the tools area I can create Gists, but that is per file and that would take a long time to go through each of the files creating them and then importing them to Github.

I guess nobody is monitoring this?

2 Likes

I also want to tackle the challenges at the end of Redux Toolkit lesson. I was hoping to get some insights from here. Please I want to know if you eventually solved those challenges

Ok I’ll make sure to post an update if/when I get back to them. :+1:

1 Like

Thanks a lot for replying. I’m eagerly waiting for your post with the updates

The example in this lesson quietly introduces the Provider component in index.js, and the useSelector method in FavoriteRecipes.js. This seems to be an important new concept for passing state directly to components as opposed to threading it down through props. Is this concept expressly covered in another lesson? I don’t see it in the syllabus. Thanks!

1 Like

I was also interested in doing the extra challenges to solidify a bit Redux Toolkit (since it’s a lot of information and an important part) before learning additional things, but don’t really know how to approach the challenges.

I was really hoping to find some insight here but it seems no one has figured it out and that the thread isn’t monitored:/

I really wish there were more exercises on the Redux sections to solidify each part a bit more before proceeding with more information and more advanced exercises

1 Like

Hi, to whom it may work, here I leave the code I used to tackle the extra challenges, not sure if it’s all correct, but, here you go anyway:

// Create the userProfileSlice.js slice
// Import createSlice
import { createSlice } from '@reduxjs/toolkit';

// Define the initialState where the user's favorite recipes will be stored
export const initialState = {
  favoriteRecipes: [],
  // Add another property to the initial state, which will be managed by the addUserCreatedRecipe reducer
  userCreatedRecipes: [],
}

// Configure the Slice with name, initialState, and reducers/actions
export const userProfileSlice = createSlice({
  name: 'userProfile',
  initialState: initialState,
  reducers: {
    addFavoriteRecipe: (state, action) => {
      state.favoriteRecipes.push({ ...action.payload, rating: 0 });
    },
    removeFavoriteRecipe: (state, action) => {
      state.favoriteRecipes = state.favoriteRecipes.filter(recipe => recipe.id !== action.payload.id);
    },
    // Add a reducer to add user-created recipes
    addUserCreatedRecipe: (state, action) => {
      state.userCreatedRecipes.push({ ...action.payload, rating: 0 });
    },
    // Reducer to rate recipes
    rateRecipe: (state, action) => {
      const { id, rating } = action.payload;
      // Find the recipe in favoriteRecipes or userCreatedRecipes
      // recipe => recipe.id === id: takes each recipe in the array and checks if recipe.id is equal to id (the id that comes in action.payload).
      const recipe = state.favoriteRecipes.find(recipe => recipe.id === id) || state.userCreatedRecipes.find(recipe => recipe.id === id);
      // If a recipe is found (recipe is not undefined), update the rating property of that recipe with the rating value that comes in action.payload.
      if (recipe) {
        recipe.rating = rating;
      }
    }
  }
})
// Export the actions and the reducer
export const { addFavoriteRecipe, removeFavoriteRecipe, addUserCreatedRecipe, rateRecipe } = userProfileSlice.actions;
export default userProfileSlice.reducer;

// The new reducer must be added in the file where the store is configured, using configureStore()
/* used in store:
import { userProfileReducer } from '../features/userProfile/userProfileSlice.js';
userProfile: userProfileReducer
*/

Hello,
I believe it can be completed using combineReducers for the recipe slices in the example (searchTerm, allRecipes, favoriteRecipes, customRecipes). Each user needs their own unique id and arrays for storing the state of the recipe. A useful starting point maybe is to separate the code for handling users and the code for handling recipes into their own reducers.