Checking AppState prior to dispatching Redux Action? -
say have redux store keeps track of appstate comprised of single 'color' variable string.
initialstate = { color: 'red' }
and action updating this:
const set_color = 'set_color'; function setcolor(color) { return { type: set_color, color }; }
and have sort of input allows user set color whatever please. (how done irrelevant)
let newcolor = <got new color somehow>
now lets user inputs 'red' (the same current state). should care if newcolor , current color differ? ie, should first check store see if newcolor different old color, , dispatch setcolor action iff color different? or should dispatch setcolor action, regardless if there's difference.
in general, easiest thing , call setcolor
again. reason keeps logic more straightforward. time user changes color via input field, code dispatch action. now, need write test case verify behavior. may sound trivial 1) adds , 2) have test case of rapidly switching , forth between 2 colors sure code works.
i filter out newcolor()
dispatch if there explicit reason so, such as:
- performance shows needed
- the ui behavior needs different in these 2 scenarios let user know haven't changed anything
- changing color has knock on side effects of related data undesirable (for example, changing color might reset shape triangle)
or similar. point being, simple thing default unless there's reason not to.
redux actions designed cheap. don't afraid of dispatching. it's similar philosophy react. render, render, render, , let framework heavy lifting.
Comments
Post a Comment