Currently injectedProps
only support an object of functions or static props we want to inject to the subscribed component. We found some cases where we needed to access to the current state to decide which action to take or pass some computed state to the dispatched event payload.
What we have:
subscribe(Component, null, {
onClick() {
// dispatch something
},
});
Example of what we suggest:
In the following case we can cover conditions that otherwise would be done directly in the UI like deciding a behaviour based on a feature flag.
subscribe(Component, null, (state) => {
return {
onClick() {
if (getFeatureSelector(state, 'FF')) {
// dispatch A
} else {
// dispatch B
}
},
}
});
Another example:
We all know that, in any event, state is accessible through coeffects, but we could dispatch events with a payload using data computed from the state selectors.
subscribe(Component, null, (state) => {
return {
onClick() {
const data = someSelector(state);
dispatch({id: 'EVENT_ID', payload: data});
},
}
});
More
The intention of this is not to start putting logic in these injected props but to gain flexibility to move some logic from the UI to these functions, if convenient.
Also, for backwards compatibility injectedProps could be expected both as a callback and an object.