After playing around with react I noticed a strange behavior while outputting array:
function bar1() {
let arr = [0, 1];
return arr.map((el) => foo(el))
}
function bar2() {
let arr = [0, 1];
arr = arr.map((el) => foo(el))
return (
{arr}
)
}
function foo(key) {
return (
<div key={key}>Foo</div>
)
}
I would expect bar1() and bar2() to output identical results, however bar2() throws error:
Objects are not valid as a React child (found: object with keys {arr}). If you meant to render a collection of children, use an array instead or wrap the object using createFragment(object) from the React add-ons.
I need to insert additional element in bar2() after map operation, that's why i can't use the simplified version.
Is it a bug related to JSX or it behaves as expected?