i'm creating component handles state redux architecture (yes, bad definition because component should delegate state app anyway).
this component accepts different props. problem comes when have handle callback. let's put practical example picture better.
let's need component <app onsubmit={() => {}} />
.
we create store when component gets mount, inner components can dispatch actions/subscribe changes/etc.
the attempt have action called submit()
. way store know there's change apply any subscriber know , call onsubmit callback.
so code like:
class app extends component { constructor (props) { super(props) this.subscriber = this.subscriber.bind(this) } componentwillmount() { this.store = createstore() this.store.subscribe(this.subscriber) } subscriber (action) { if (action.type === 'submit') this.props.onsubmit() } render () { return ( <provider store={this.store}> <foocomponent /> </provider> ) } }
the problem that, although subscriber
called on every action dispatched, subscriber can't know last action dispatched. there's no way know when call onsubmit
.
- how can use
subscriber
know changes on store? - should stop trying redux arquitecture in here?
this anti-pattern in redux.
there no such thing “the last action” because redux reserves right notify once in case of several nested dispatches.
instead, should perform local side effect together action. redux thunk popular way this. if need more decoupling, might enjoy redux saga instead.
Comments
Post a Comment