[Redux] Combine and sort Two Pieces of State
I'm working on a web app where we make a call to the Twitter API and our news API, the app's news feed needs to feature both entries sorted by date in the same data set.
I'm curious what would be considered the best approach, currently I'm using Promises.all on my twitter and news actions then running a sorting utility function in my component. This doesn't feel very "reduxy", would it make more sense to create a reducer which listens for TWEETS_SUCCESS and POSTS_SUCCESS then sort them into a piece of state?
I should note the long term goal is to cache and sort the responses server side in our DB.
Posted Oct 27, 2016, 3:23 PM UTC
03
The standard suggested approach for managing data in Redux is to store data in a "normalized" structure with objects as lookup tables mapping IDs to items, and then use arrays of IDs to indicate ordering. You can potentially do the sorting in three main places: before dispatching an action (in an action creator), in a reducer, or in "selector" functions that derive data from what's in the store. Any of those are viable, but the usual advice is to store the minimal amount of info in the store, and derive from that as needed.
You may want to read through several sections in the Redux docs: Computing Derived Data, FAQ: Organizing data in the state, and the new Structuring Reducers section.
Thanks, these entries are really helpful. Also thanks for maintaining your Redux/React repos!
Sure. Always nice to hear that they're proving useful and helping people :)