0

I'm using the connect function React Redux to connect my store to a React component. After it is connected I want to render the component. This works:

class Init extends React.Component {
  render() {
    return (
      <View /> // Is really more elaborate
    )
  }
}

Connect it to the store:

const InitPage = connect(
  state => ({
  }),
  dispatch => ({
  })
)(Init)

Now render it:

class App extends React.Component {
  render() {
    const content = <InitPage />

    return (
      {content}
    )
  }
}

Great. This all works. However...

When I place the connect inside a function and return the connect, as in:

const getInitPage = () => {
  const InitPage = connect(
    state => ({
    }),
    dispatch => ({
    })
  )(Init)

  return InitPage
}

If I now try to render the component:

class App extends React.Component {
  render() {
    const content = getInitPage()

    return (
      {content}
    )
  }
}

I get an error:

Invariant Violation: React.Children.only expected to receive a single React element child.

What is the correct way to return a Redux "connected" component from a function?

1 Answer 1

1

In case you are returning the component from the function, you need to render it like

class App extends React.Component {
  render() {
    const Content = getInitPage()

    return (
      <Content/>
    )
  }
}

Also make sure the componentName starts with a uppercase character.

Sign up to request clarification or add additional context in comments.

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.