Quick question. Anyone know how to render an array of components? Trying to make it easier for a developer to alter a particular component. (It's like a dashboard).
Component list file
import React from 'react';
export default [
<ComponentOne/>
<ComponentTwo/>
];
Dashboard Component
import React from 'react';
import components from './../../components';
export default class Dashboard extends React.Component
{
render = () => {
//Want to render the array of components here.
return (
<div className="tile is-parent">
{components}
</div>
);
};
}
The issue is I have an array of components that I need to add a key to. However! I can't seem to add a key to the component as well, not sure how to explain it really so here's the code I've tried:
{components.map((component, key) => (
<component key={key}/>
}
If I do the above I get no 'you must apply a key' errors however nothing renders? And I'm guessing it's because 'component' doesn't exist or something weird along those lines.
I've also tried component.key = key; but it doesn't let me do that on this type of Object apparently?
My fallback I suppose is to return a shorthand function instead of an array but I like the array for some reason? Seems simpler for juniors.
componentas it will taken in as an attribute to the component.