Last active
September 25, 2019 13:20
-
-
Save coltpini/853491234babd3f7708832f28f5188cc to your computer and use it in GitHub Desktop.
This file contains 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
//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} /> | |
} | |
} |
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
Why are the
mapActionsToDispatch
andmergeProps
function bodies wrapped in parenthesis?