19

"@types/react": "^16.7.17"
"@types/react-dom": "^16.0.11"
"typescript": "^3.2.2"

function ArryElement() {
  return [
    <div key='1'>1</div>,
    <div key='2'>2</div>
  ];
}

function App() {
  return <ArryElement />
}

2
  • 4
    It seems typescript does not allow you to return an array of elements from render. Simply use React.Fragment instead. Commented Jan 3, 2019 at 8:48
  • Possible duplicate of Return react 16 array elements in typescript Commented Jan 3, 2019 at 9:01

1 Answer 1

27

This is because React is expecting to return an object. What you do in this example, is return an array instead.

As a user mentioned above, wrapping it in a Fragment (which is an object) will solve the case

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.