1

I have that part of code

const links = [
  {
    name: 'How it works',
    ref: '/'
  },
  {
    name: 'Calendar',
    ref: 'calendar'
  },
  {
    name: 'Contact me',
    ref: 'contact'
  }
];

const renderLinks = links.map((link, index) =>
  <li className="nav-item active" key={index}>
    <a className="nav-link" href={link.ref || "#"}>
      {link.name}
    </a>
  </li>
);

However when I try to render it an error is thrown.

render() {
    return (
      {renderLinks}
    );
  }

Objects are not valid as a React child (found: object with keys {renderLinks}). If you meant to render a collection of children, use an array instead.

As I think, I have to got an array but React thinks there is an object.

2
  • 1
    return renderLinks; Commented Apr 13, 2018 at 19:59
  • @dfsq wow... Thanks a lot. Solved my problem. Feel free to post an answer I'll check it. Commented Apr 13, 2018 at 20:01

1 Answer 1

2

React thinks this is an object because you indeed provided an object. This is what it is if you write it without shortcut property notation:

render() {
  return {
    renderLinks: renderLinks
  );
}

Just return renderLinks directly without { }:

render() {    
  return renderLinks;
}
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.