Oluwaseunus / anime-characters

Anime Characters Web App for a test :fire:
0 stars 0 forks source link

There's absolutely no need for more than 2 reducers #9

Open ooade opened 5 years ago

ooade commented 5 years ago

I see you have animes reducer, currentCharacter reducer, and singleAnime reducer.

animes and singleAnimes have same action types FETCH_ALL_ANIME_STARTED. Tho, there won't be any issue while trying to retrieve them from the component since they'd be exported differently in the combinedReducer.

One thing you could do is, have just anime(s) and character(s) reducers, and have action types like:

and so.

And, instead of having your reducer state as array, it could be an object, so you could do all of these easily.

It's all good. Besides, you can correct me if I'm wrong.

Thanks, try to give me feedback on the others and if you have any thing to say on this, feel free.

Oluwaseunus commented 5 years ago

The different reducers are results of the API having separate endpoints for all three. Also, the FETCH_ALL_ANIME_STARTED type was added to the singleAnimes reducer because when viewing a single anime from the main page, the data from the previous anime is still there. So although the component will fetch the data in the background, when the singleAnime component mounts, it opens immediately with data of the previous anime. Adding the type was to make sure the page wouldn't first show info from another show.

ooade commented 5 years ago

I get you totally, but like I said, the plan is not taking the data away but using an object to house them. So you could have your initialState as:

const state = {
   anime: null, // current anime
   animes: [], // list of all animes
}

So when you try to update the state, you'd do:

return { ...state, anime: currentAnimePayload }
// or
return { ...state, animes: allAnimesPayload }

Nothing stops those 2 from co-existing.

And besides, let's say we have pagination, and we try adding more animes. We could still do:

return { ...state, animes: [...state.animes, ...allNewAnimesPayload] }

Imagine going your way in a large repository, it means we'd have a lot of global states scattered around and it won't be easy to manage.

Oluwaseunus commented 5 years ago

Oh I get what you mean now. This makes sense since it works well too. Thank you.