Created
December 5, 2017 09:52
-
-
Save jacobp100/bc2fa8c719a938ba02b1ad91e330526b to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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