flux-react-router-example
This is a sample Flux app I wrote on a weekend.
It uses open Github API to display starred repos by users and stargazers by repo.
I made it to document a few approaches I have tried while learning Flux.
I tried to keep it close to real world (pagination, no fake localStorage APIs).
There are a few bits here I was especially interested in:
- It uses Flux architecture and react-router;
- It can show user page with partial known info and load details on the go;
- It supports pagination both for users and repos;
- It parses Github's nested JSON responses with normalizr;
- Content Stores don't need to contain a giant
switch
with actions; - “Back” is immediate (because all data is in Stores);
- Router handlers are updated gracefully in
componentWillReceiveProps
if some page is requested with a different parameter.
Running
npm install
npm start
How I Classify Stores
I tried to avoid some of the duplication I've seen in other Flux example, specifically in Stores. I found it useful to logically divide Stores into three categories:
Content Stores hold all app entities. Everything that has an ID needs its own Content Store. Components that render individual items ask Content Stores for the fresh data.
Content Stores harvest their objects from all server actions. For example, UserStore
looks into action.response.entities.users
if it exists regardless of which action fired. There is no need for a switch
. Normalizr makes it easy to flatten any API reponses to this format.
// Content Stores keep their data like this 7: id: 7 name: 'Dan' ...
List Stores keep track of IDs of entities that appear in some global list (e.g. “feed”, “your notifications”). In this project, I don't have such Stores, but I thought I'd mention them anyway. They handle pagination.
They normally respond to just a few actions (e.g. REQUEST_FEED
, REQUEST_FEED_SUCCESS
, REQUEST_FEED_ERROR
).
// Paginated Stores keep their data like this7 10 5 ...
Indexed List Stores are like List Stores but they define one-to-many relationship. For example, “user's subscribers”, “repository's stargazers”, “user's repositories”. They also handle pagination.
They also normally respond to just a few actions (e.g. REQUEST_USER_REPOS
, REQUEST_USER_REPOS_SUCCESS
, REQUEST_USER_REPOS_ERROR
).
In most social apps, you'll have lots of these and you want to be able to quickly create one more of them.
// Indexed Paginated Stores keep their data like this 2: 7 10 5 ... 6: 7 1 2 ... ...
Note: these are not actual classes or something; it's just how I like to think about Stores. I made a few helpers though.
StoreUtils
createStore
This method gives you the most basic Store:
{ var store = ; _; store; return store;}
I use it to create all Stores.
isInBag
, mergeIntoBag
Small helpers useful for Content Stores.
{ var item = bagid; if !bagid return false; if fields return fields; else return true; } { if !transform x; for var key in entities if !entities continue; if !bag bagkey = ; else if ! bagkey = ; }
PaginatedList
Stores pagination state and enforces certain assertions (can't fetch page while fetching, etc).
{ this_ids = ids || ; this_pageCount = 0; this_nextPageUrl = null; this_isExpectingPage = false; } { return this_ids; } { return this_pageCount; } { return this_isExpectingPage; } { return this_nextPageUrl; } { return this === null && this > 0; } { this_ids = _; } { this_ids = _; } { ; this_isExpectingPage = true; } { ; this_isExpectingPage = false; } { ; if newIdslength this_ids = _; this_isExpectingPage = false; this_nextPageUrl = nextPageUrl || null; this_pageCount++; }
PaginatedStoreUtils
createListStore
, createIndexedListStore
, createListActionHandler
Makes creation of Indexed List Stores as simple as possible by providing boilerplate methods and action handling:
var PROXIED_PAGINATED_LIST_METHODS = 'getIds' 'getPageCount' 'getNextPageUrl' 'isExpectingPage' 'isLastPage'; { var spec = getList: getList ; PROXIED_PAGINATED_LIST_METHODS; return spec;} /** * Creates a simple paginated store that represents a global list (e.g. feed). */ { var list = ; { return list; } { return listmethod; } return ;} /** * Creates an indexed paginated store that represents a one-many relationship * (e.g. user's posts). Expects foreign key ID to be passed as first parameter * to store methods. */ { var lists = {}; { if !listsid listsid = ; return listsid; } { var id = args; if typeof id === 'undefined' throw 'Indexed pagination store methods expect ID as first parameter.'; var list = ; return listmethod; } return ;} /** * Creates a handler that responds to list store pagination actions. */ { var request: requestAction error: errorAction success: successAction = actions; ; ; ; return { };} var PaginatedStoreUtils = createListStore: createListStore createIndexedListStore: createIndexedListStore createListActionHandler: createListActionHandler;
createStoreMixin
A mixin that allows components to tune in to Stores they're interested in, e.g. mixins: [createStoreMixin(UserStore)]
.
{ var StoreMixin = { return this; } { stores; this; } { stores; } { if this this; } ; return StoreMixin;}