Skip to content

Instantly share code, notes, and snippets.

@jacobp100
Created December 5, 2017 09:52
Show Gist options
  • Save jacobp100/bc2fa8c719a938ba02b1ad91e330526b to your computer and use it in GitHub Desktop.
Save jacobp100/bc2fa8c719a938ba02b1ad91e330526b to your computer and use it in GitHub Desktop.
const getAction = ({ action, state }) =>
typeof action === "function" ? action(state) : action
const Dispatch = connect(state => ({ state }))(
class Dispatch extends Component {
componentWillMount() {
const currentAction = getAction(this.props)
if (currentAction != null) this.props.dispatch(currentAction)
}
componentWillReceiveProps(nextProps) {
const currentAction = getAction(this.props)
const nextAction = getAction(nextProps)
if (nextAction != null && !isEqual(currentAction, nextAction)) {
this.props.dispatch(nextAction)
}
}
render() {
return null
}
},
)
const Ex = () => (
<Example>
<Dispatch action={loginUser()} />
<Dispatch action={state => fetchData(state.something)} />
<Route path="/:id">
{({ match }) => <Dispatch action={match ? setActiveView(match.params.id) : null} />}
</Route>
</Example>
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment