2019/08/20
redux, store, state, pattern
Redux is set of very simple but also very useful ideas:
And when combined with a declarative UI like React, the result can be quite good.
Some history of our Redux&React
journey:
When starting with
React
andRedux
, most of us may spend a lot of time focusing on syntax, and the somewhat repetitive 'boilerplate'.Then the initial confusing phase pass, the syntax works, the code piles up, the first rev of UI is done. But the UI tree feels wired and shaky:
props
(data) passing is tedious and non-obvious, and many function gets passed down, some ref gets returned up, causing confusing flow of control- the leaf
Component
have to much code for both UI and data logic, the most of the branchComponent
just passing & filtering dataThen it occurs to us: maybe it's time to finally fully complete the docs for
Redux
andReact
, seriously, and no skipping.And from the doc, push the UI code down, lift the data and control code up the tree is a good idea, also clear up the lazy naming for some of the
props
&Component
helps, too.The second rev of UI survives more iteration, during which one or more redux
middleware
is introduced for async actions, store state gets more crowded, the code works, still somewhat readable, but still hard to discuss.Then the ideas of modularize of UI and data code comes, most of the time, the UI is not one big problem, but several smaller separated module problem to solve. The code split up for each module, with code for both the UI and data.
So a better code structure is designed: some module each with a smaller store, and a smaller UI tree, then some wrapper code to compose these up.
what we ended up with is modular data & UI code:
and some opinionated extra simplification/modification:
reducer
code logic is minimal:
just merge the action payload, and expect the dispatcher generate the correct nextStatemiddleware
, this way,
the UI dispatch is simple, the state logic can be put into one file,
and the code can be asyncobject
, no function ot promise,
so action should always be simple and descriptivebasically we valued:
state
reduce/producedispatch
reducer
and Redux
codefor the
middleware
, search forredux-entry
React&Redux
worked for us because:
action
, state
, and UI
is useful,
code reuse and code responsibility is manageableThe concept of store is not limited to React or frontend only
We can generalize UI to be some visible output, or just output, then a common data flow with store can be like:
action -> store & state -> output
we may apply the redux-like store code structure to some different scenario:
request -> server & data -> response
we may create a store
for each incoming request
,
process it by passing though layers of reducer
updating the store state
,
and finally generate a response
event/message -> eventListener/messageQueue -> some change
for some complex stateful event/messaging code,
maybe create a store with state,
and put message-processing/state-changing code in some reducer
,
can result in a more clear and debuggable code