Can you create an array of React elements eg something like:
const Component = () => {
const myArray = [{<p>Hi</p>}]
return(
// Stuff
)
}
Can you create an array of React elements eg something like:
const Component = () => {
const myArray = [{<p>Hi</p>}]
return(
// Stuff
)
}
Sure.
const myArray = [<p>1</p>, <p>2</p>, <p>3</p>];
// Fragment is included because everything in return needs to be wrapped in a single tag
return (
<Fragment>{ myArray }</Fragment>
)
Also note that {'s are used to embed logic inside JSX
const foo1 = 'foo';
const foo2 = 'bar';
const myArray = [<p>{foo1}</p>, <p>{foo2}</p>];
return (
<Fragment>
{myArray.map((item, index) => (
<span key={index}>
<h1>{ 'Element #' + index }</h1>
{item}
</span>
)}
</Fragment>
)