It is my first time using useReducer() hook and I am facing a problem where I need to pass it a value as argument.
Here is how my reducer looks like:
const memoizedReducer = useCallback((state, action) => {
switch (action.type) {
case "save":
const refreshedBookData = {
...state.bookData,
...(state.bookData.totalSaves >= 0 && {
totalSaves: state.bookData.totalSaves + 1,
}),
isSaved: true,
};
// Add the new book data to a Map which I have in a Context provider
currentUser.addBookToVisitedBooks(bookId, refreshedBookData);
// Update my context provider data
currentUser.setData((prevCurrentUserData) => {
return {
...prevCurrentUserData,
...(prevCurrentUserData.totalSaves >= 0 && {
totalSaves: prevCurrentUserData.totalSaves + 1,
}),
};
});
return refreshedBookData;
case "unsave":
const refreshedBookData = {
...state.bookData,
...(state.otheBookData.totalSaves >= 0 && {
totalSaves: state.bookData.totalSaves - 1,
}),
isSaved: false,
};
// Add the new book data to a Map which I have in a Context provider
currentUser.addBookToVisitedBooks(bookId, refreshedBookData);
// Update my context provider data
currentUser.setData((prevCurrentUserData) => {
return {
...prevCurrentUserData,
...(prevCurrentUserData.totalSaves > 0 && {
totalSaves: prevCurrentUserData.totalSaves - 1,
}),
};
});
return refreshedBookData;
default:
return state;
});
const [{ bookData }, dispatch] = useReducer(memoizedReducer, {
bookData: params?.bookData
});
As you can see, what I am doing is:
1- If the action type is "save", increase the total saves for the book, add the new book data to a Map of "visitedBooks" which I have in a Context (ignore this part), and update my currentUser data increasing his total saves.
2- If the action type is "unsave", I do the opposite.
My problem is that I need to pass an argument "reviews" to the reducer. For example, if I have this function for fetching "extra data" from my db:
const fetchReviews = async (bookId) => {
// Get a list of reviews from the db
const reviews = await db.fetchReviews(bookId);
return reviews;
}
and I use it like this:
const reviews = await fetchReviews(bookId);
how can I pass the reviews as argument to the reducer?
dispatch({ type: saved ? "save" : "unsave" });
Thank you.