0

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);

2 Answers 2

2

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;
  }
} 
}
Sign up to request clarification or add additional context in comments.

Comments

2

DOCS:

Parenthesize the body to return an object literal expression:

const AppConnected = connect(state => ({text: state.text}))(App);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.