Reputation: 1613
I am playing around with React and Redux and ran into some issues with connect.
const AppConnected = connect((state) => {text: state.text})(App);
In the first example, I get the error message Cannot get 'text' of undefined
whereas the second example runs without problems. What is the reason behind this?
const AppConnected = connect((state) => {
return {
text: state.text
}
})(App);
Upvotes: 0
Views: 1194
Reputation: 53991
You're not returning an object in the first example like you think you are. You're defining the body of a function with a label called text
.
To return an object literal from an arrow you need to wrap it in ()
.
(state) => ({})
Labels in JavaScript are used to control the flow of execution. It's not a recommended pattern but works like this:
function () {
text: while(someCondition){ // Label
if (someOtherCondition) {
continue text;
}
}
}
Upvotes: 2
Reputation: 3375
Parenthesize the body to return an object literal expression:
const AppConnected = connect(state => ({text: state.text}))(App);
Upvotes: 2