redux - Why `mapDispatchToProps` does not see `stateProps`? -
intro
the signature of mapdispatchtoprops
is:
mapdispatchtoprops(dispatch, [ownprops]): dispatchprops
one wonder, why mapdispatchtoprops
not provided stateprops
i.e. result of mapstatetoprops
call. alternative syntax such as:
mapdispatchtoprops(dispatch, [ownprops], [stateprops]): dispatchprops
it seems me not break (not performance) , can beneficial in situations.
question
is there reason why not implemented? missing something? how solve use case presented below without feature?
why not break anything
such change allow user customize dispatchprops
functions stateprops
. when stateprops
change, react-redux can recompute dispatchprops
actual stateprops
, rerender component. such logic happens ownprops
performance
if user not need read stateprops
, can omit argument in mapdispatchtoprops
implementation. react-redux can detect , assume, dispatchprops
not dependent on stateprops
. such performance tweak implemented ownprops
.
use case
say have 10 action(creator)s deal specific todo item. these 10 actions looks like:
changetitle(todoid, title) { return { type: 'changetitle', todoid, title, } } // etc...
if 10 action(creator)s need todoid
1 might make sense make partial instantiation of action(creator)s (it can written more succinctly that's not relevant now):
actionsfortodo(todoid) { return { changetitle: (title) => ({ type: 'changetitle', todoid, title, }), // more action(creator)s here } }
and provide todoid
action(creator)s:
mapdispatchtoprops((dispatch, ownprops, stateprops) => bindactioncreators(actionsfortodo(stateprops.todoid)))
this would negatively affect performance quite bit. since result of mapstate
function of store state , possibly component's own incoming props, require mapdispatch
re-run every time output of mapstate changes
, , result in new function objects being allocated every single time. waste of cpu cycles in cases. it's simpler pass in item-specific values arguments when call these functions component.
there is supported workaround: third argument connect()
mergeprops
function, does the output of mapstate
, mapdispatch
, , can customize how they're merged together. if use case matters you, can implement logic providing mergeprops
function.
that said, upcoming react-redux@5.0.0 prerelease version major rewrite of implementation, does have new connectadvanced
function supports type of scenario better. see https://github.com/reactjs/react-redux/pull/416 .
Comments
Post a Comment