Skip to content

Instantly share code, notes, and snippets.

@coltpini
Last active September 25, 2019 13:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save coltpini/853491234babd3f7708832f28f5188cc to your computer and use it in GitHub Desktop.
Save coltpini/853491234babd3f7708832f28f5188cc to your computer and use it in GitHub Desktop.
//Step 1: Pull in the function for the action.
import { getArticles } from '../store/articleList/actions.js';
//Step 2: This connects the action, getArticles, to dispatch the action.
const mapActionsToDispatch = (dispatch) => ({
loadArticles: () => dispatch( getArticles() )
});
//Step 3: This takes the connected action and merges it to a prop for React.
const mergeProps = (state, actions) => ({
...state,
...actions,
loadArticles: () => actions.loadArticles()
});
//Step 4: Merging it to a prop makes it available in the component,
// in this case on the componentDidMount life cycle event.
// This could just as easily be a click, submit, type, or other type of event.
class LandingContainer extends ReactComponent {
componentDidMount() {
const {loadArticles} = this.props;
loadArticles();
}
render() {
return <Landing {...this.props} />
}
}
@myoffe
Copy link

myoffe commented Nov 19, 2017

Why are the mapActionsToDispatch and mergeProps function bodies wrapped in parenthesis?

@coltpini
Copy link
Author

Sorry, I just saw this a few years later.
They are wrapped in parens because they are implicitly returning objects. In order to do that from an arrow function you have to wrap them like this:

(oneProp, twoProp) => ({ redProp, blueProp})
// or expanded
(oneProp, twoProp) => ({redProp: redProp, blueProp: blueProp})

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment