React render array of components
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 obviously it's an array of components so I need to add a key. However! I can't seem how 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 maybe 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 return a shorthand function instead of an array but I like the array for some reason? Seems simpler for juniors.
arrays reactjs
add a comment |
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 obviously it's an array of components so I need to add a key. However! I can't seem how 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 maybe 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 return a shorthand function instead of an array but I like the array for some reason? Seems simpler for juniors.
arrays reactjs
You can't apply key tocomponent
as it will taken in as an attribute to the component.
– John Kennedy
Jan 6 '18 at 19:41
add a comment |
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 obviously it's an array of components so I need to add a key. However! I can't seem how 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 maybe 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 return a shorthand function instead of an array but I like the array for some reason? Seems simpler for juniors.
arrays reactjs
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 obviously it's an array of components so I need to add a key. However! I can't seem how 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 maybe 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 return a shorthand function instead of an array but I like the array for some reason? Seems simpler for juniors.
arrays reactjs
arrays reactjs
asked Jan 6 '18 at 19:32
sourRaspberrisourRaspberri
4,76621728
4,76621728
You can't apply key tocomponent
as it will taken in as an attribute to the component.
– John Kennedy
Jan 6 '18 at 19:41
add a comment |
You can't apply key tocomponent
as it will taken in as an attribute to the component.
– John Kennedy
Jan 6 '18 at 19:41
You can't apply key to
component
as it will taken in as an attribute to the component.– John Kennedy
Jan 6 '18 at 19:41
You can't apply key to
component
as it will taken in as an attribute to the component.– John Kennedy
Jan 6 '18 at 19:41
add a comment |
6 Answers
6
active
oldest
votes
Have you consider using the new React Fragments? (in v16)
This would be the simplest solution as it would by pass the whole array/key issue.
If you need to pass key, then I'd suggest to simply require the components to have the keys. This is how React works, so I wouldn't suggest you to hide this behavior behind an interface that might not be predictable.
If you really need to do this, then you can use React.cloneElement
to clone the element and inject new properties:
React.cloneElement(element, { key: 'foo' });
add a comment |
If you’re always going to want to render all the components in your components file then you’re probably better off wrapping them in a React.Fragments tag.
Best practise is just to export this as a simple function that returns the components rather than as a constant.
So...
const Components = props => {
return (
<React.Fragment>
<ComponentOne>
<ComponentTwo>
<React.Fragment>
)
}
export default Components
That allows you to put multiple components next to each other without a DOM element containing them.
You should then just be able to render that by using it as a normal component and it’ll render all of them, so just import it then...
<Components >
Otherwise, if you want to treat them like an array, you have a function for free on the React object you’ve imported...
React.Children.toArray(arrayOfComponents)
You pass it an array of components (like in your original question) and it allows you to sort and slice it if you need to then you should be able to just drop it in the return of your render function
add a comment |
It's pretty easy, just wrap your component into div
and pass key
there as i did below:
const Example = ({components}) => (
<div>
{components.map((component, i) => <div key={i}>{component}</div>)}
</div>
)
Worked example
Looking at your worked example. How could I make this work if my render section looked like this. wherevar items = [Foo, Baz]; let items: any = items.map(item => { //alert(tab); return ({item}); });ReactDOM.render( <Example components={items}/>, document.getElementById('container') );
– jaxkewl
Jan 14 at 19:20
sorry for the messy syntax, i made a fiddle that does not work. jsfiddle.net/208y7mwg
– jaxkewl
Jan 14 at 19:27
add a comment |
Actually you are exporting array of elements
from the file. One way is to export array of component and render them like
import React from 'react';
export default [
ComponentOne
ComponentTwo
];
// Then following will work
{components.map((Component, key) => (
// Remember to make first letter capital (in this case "c")
<Component key={key}/>
}
The other way is to wrap the component in div
like this
import React from 'react';
export default [
<ComponentOne/>
<ComponentTwo/>
];
// Then wrap in div
{components.map((component, key) => (
<div key={key}>
{component}
</div>
}
the second way will not work, bc you cannot return something like this< <Component/>/>
– The Reason
Jan 6 '18 at 19:45
@TheReason And the reason?
– Prakash Sharma
Jan 6 '18 at 19:47
just updated a comment, you have to remove<
&/>
from elements of array component. Does it make sense?
– The Reason
Jan 6 '18 at 19:48
add a comment |
Following up with my comment, you should be doing this instead:
{components.map((component, index) => (
<span key={index}>
{ component }
</span>
}
With React 16, you can use React.Fragment
:
{components.map((component, index) => (
<React.Fragment key={index}>
{ component }
</React.Fragment>
}
1
Rather than using a span, you could also useReact.Fragment
to prevent having to add a parent container: <React.Fragment key={index}>{component}</React.Fragment>
– Craig Myles
Nov 16 '18 at 0:08
add a comment |
All of these answers are almost right. Just remove the <../>
from your exports:
export default [
ComponentOne,
ComponentTwo,
]
And in the other file use .map()
:
export default class Dashboard extends React.Component {
render = () => (
<div className="tile is-parent">
{components.map((Component, key) => (<Component key={key} />))}
</div>
)
}
Also note that if you wanted to use Fragment
like others suggested you can just write <>...</>
instead.
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f48131100%2freact-render-array-of-components%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
6 Answers
6
active
oldest
votes
6 Answers
6
active
oldest
votes
active
oldest
votes
active
oldest
votes
Have you consider using the new React Fragments? (in v16)
This would be the simplest solution as it would by pass the whole array/key issue.
If you need to pass key, then I'd suggest to simply require the components to have the keys. This is how React works, so I wouldn't suggest you to hide this behavior behind an interface that might not be predictable.
If you really need to do this, then you can use React.cloneElement
to clone the element and inject new properties:
React.cloneElement(element, { key: 'foo' });
add a comment |
Have you consider using the new React Fragments? (in v16)
This would be the simplest solution as it would by pass the whole array/key issue.
If you need to pass key, then I'd suggest to simply require the components to have the keys. This is how React works, so I wouldn't suggest you to hide this behavior behind an interface that might not be predictable.
If you really need to do this, then you can use React.cloneElement
to clone the element and inject new properties:
React.cloneElement(element, { key: 'foo' });
add a comment |
Have you consider using the new React Fragments? (in v16)
This would be the simplest solution as it would by pass the whole array/key issue.
If you need to pass key, then I'd suggest to simply require the components to have the keys. This is how React works, so I wouldn't suggest you to hide this behavior behind an interface that might not be predictable.
If you really need to do this, then you can use React.cloneElement
to clone the element and inject new properties:
React.cloneElement(element, { key: 'foo' });
Have you consider using the new React Fragments? (in v16)
This would be the simplest solution as it would by pass the whole array/key issue.
If you need to pass key, then I'd suggest to simply require the components to have the keys. This is how React works, so I wouldn't suggest you to hide this behavior behind an interface that might not be predictable.
If you really need to do this, then you can use React.cloneElement
to clone the element and inject new properties:
React.cloneElement(element, { key: 'foo' });
answered Jan 6 '18 at 19:45
Simon BoudriasSimon Boudrias
29.5k975111
29.5k975111
add a comment |
add a comment |
If you’re always going to want to render all the components in your components file then you’re probably better off wrapping them in a React.Fragments tag.
Best practise is just to export this as a simple function that returns the components rather than as a constant.
So...
const Components = props => {
return (
<React.Fragment>
<ComponentOne>
<ComponentTwo>
<React.Fragment>
)
}
export default Components
That allows you to put multiple components next to each other without a DOM element containing them.
You should then just be able to render that by using it as a normal component and it’ll render all of them, so just import it then...
<Components >
Otherwise, if you want to treat them like an array, you have a function for free on the React object you’ve imported...
React.Children.toArray(arrayOfComponents)
You pass it an array of components (like in your original question) and it allows you to sort and slice it if you need to then you should be able to just drop it in the return of your render function
add a comment |
If you’re always going to want to render all the components in your components file then you’re probably better off wrapping them in a React.Fragments tag.
Best practise is just to export this as a simple function that returns the components rather than as a constant.
So...
const Components = props => {
return (
<React.Fragment>
<ComponentOne>
<ComponentTwo>
<React.Fragment>
)
}
export default Components
That allows you to put multiple components next to each other without a DOM element containing them.
You should then just be able to render that by using it as a normal component and it’ll render all of them, so just import it then...
<Components >
Otherwise, if you want to treat them like an array, you have a function for free on the React object you’ve imported...
React.Children.toArray(arrayOfComponents)
You pass it an array of components (like in your original question) and it allows you to sort and slice it if you need to then you should be able to just drop it in the return of your render function
add a comment |
If you’re always going to want to render all the components in your components file then you’re probably better off wrapping them in a React.Fragments tag.
Best practise is just to export this as a simple function that returns the components rather than as a constant.
So...
const Components = props => {
return (
<React.Fragment>
<ComponentOne>
<ComponentTwo>
<React.Fragment>
)
}
export default Components
That allows you to put multiple components next to each other without a DOM element containing them.
You should then just be able to render that by using it as a normal component and it’ll render all of them, so just import it then...
<Components >
Otherwise, if you want to treat them like an array, you have a function for free on the React object you’ve imported...
React.Children.toArray(arrayOfComponents)
You pass it an array of components (like in your original question) and it allows you to sort and slice it if you need to then you should be able to just drop it in the return of your render function
If you’re always going to want to render all the components in your components file then you’re probably better off wrapping them in a React.Fragments tag.
Best practise is just to export this as a simple function that returns the components rather than as a constant.
So...
const Components = props => {
return (
<React.Fragment>
<ComponentOne>
<ComponentTwo>
<React.Fragment>
)
}
export default Components
That allows you to put multiple components next to each other without a DOM element containing them.
You should then just be able to render that by using it as a normal component and it’ll render all of them, so just import it then...
<Components >
Otherwise, if you want to treat them like an array, you have a function for free on the React object you’ve imported...
React.Children.toArray(arrayOfComponents)
You pass it an array of components (like in your original question) and it allows you to sort and slice it if you need to then you should be able to just drop it in the return of your render function
answered Jan 6 '18 at 20:00
Matt WillsMatt Wills
1026
1026
add a comment |
add a comment |
It's pretty easy, just wrap your component into div
and pass key
there as i did below:
const Example = ({components}) => (
<div>
{components.map((component, i) => <div key={i}>{component}</div>)}
</div>
)
Worked example
Looking at your worked example. How could I make this work if my render section looked like this. wherevar items = [Foo, Baz]; let items: any = items.map(item => { //alert(tab); return ({item}); });ReactDOM.render( <Example components={items}/>, document.getElementById('container') );
– jaxkewl
Jan 14 at 19:20
sorry for the messy syntax, i made a fiddle that does not work. jsfiddle.net/208y7mwg
– jaxkewl
Jan 14 at 19:27
add a comment |
It's pretty easy, just wrap your component into div
and pass key
there as i did below:
const Example = ({components}) => (
<div>
{components.map((component, i) => <div key={i}>{component}</div>)}
</div>
)
Worked example
Looking at your worked example. How could I make this work if my render section looked like this. wherevar items = [Foo, Baz]; let items: any = items.map(item => { //alert(tab); return ({item}); });ReactDOM.render( <Example components={items}/>, document.getElementById('container') );
– jaxkewl
Jan 14 at 19:20
sorry for the messy syntax, i made a fiddle that does not work. jsfiddle.net/208y7mwg
– jaxkewl
Jan 14 at 19:27
add a comment |
It's pretty easy, just wrap your component into div
and pass key
there as i did below:
const Example = ({components}) => (
<div>
{components.map((component, i) => <div key={i}>{component}</div>)}
</div>
)
Worked example
It's pretty easy, just wrap your component into div
and pass key
there as i did below:
const Example = ({components}) => (
<div>
{components.map((component, i) => <div key={i}>{component}</div>)}
</div>
)
Worked example
answered Jan 6 '18 at 19:47
The ReasonThe Reason
4,70531230
4,70531230
Looking at your worked example. How could I make this work if my render section looked like this. wherevar items = [Foo, Baz]; let items: any = items.map(item => { //alert(tab); return ({item}); });ReactDOM.render( <Example components={items}/>, document.getElementById('container') );
– jaxkewl
Jan 14 at 19:20
sorry for the messy syntax, i made a fiddle that does not work. jsfiddle.net/208y7mwg
– jaxkewl
Jan 14 at 19:27
add a comment |
Looking at your worked example. How could I make this work if my render section looked like this. wherevar items = [Foo, Baz]; let items: any = items.map(item => { //alert(tab); return ({item}); });ReactDOM.render( <Example components={items}/>, document.getElementById('container') );
– jaxkewl
Jan 14 at 19:20
sorry for the messy syntax, i made a fiddle that does not work. jsfiddle.net/208y7mwg
– jaxkewl
Jan 14 at 19:27
Looking at your worked example. How could I make this work if my render section looked like this. where
var items = [Foo, Baz]; let items: any = items.map(item => { //alert(tab); return ({item}); });ReactDOM.render( <Example components={items}/>, document.getElementById('container') );
– jaxkewl
Jan 14 at 19:20
Looking at your worked example. How could I make this work if my render section looked like this. where
var items = [Foo, Baz]; let items: any = items.map(item => { //alert(tab); return ({item}); });ReactDOM.render( <Example components={items}/>, document.getElementById('container') );
– jaxkewl
Jan 14 at 19:20
sorry for the messy syntax, i made a fiddle that does not work. jsfiddle.net/208y7mwg
– jaxkewl
Jan 14 at 19:27
sorry for the messy syntax, i made a fiddle that does not work. jsfiddle.net/208y7mwg
– jaxkewl
Jan 14 at 19:27
add a comment |
Actually you are exporting array of elements
from the file. One way is to export array of component and render them like
import React from 'react';
export default [
ComponentOne
ComponentTwo
];
// Then following will work
{components.map((Component, key) => (
// Remember to make first letter capital (in this case "c")
<Component key={key}/>
}
The other way is to wrap the component in div
like this
import React from 'react';
export default [
<ComponentOne/>
<ComponentTwo/>
];
// Then wrap in div
{components.map((component, key) => (
<div key={key}>
{component}
</div>
}
the second way will not work, bc you cannot return something like this< <Component/>/>
– The Reason
Jan 6 '18 at 19:45
@TheReason And the reason?
– Prakash Sharma
Jan 6 '18 at 19:47
just updated a comment, you have to remove<
&/>
from elements of array component. Does it make sense?
– The Reason
Jan 6 '18 at 19:48
add a comment |
Actually you are exporting array of elements
from the file. One way is to export array of component and render them like
import React from 'react';
export default [
ComponentOne
ComponentTwo
];
// Then following will work
{components.map((Component, key) => (
// Remember to make first letter capital (in this case "c")
<Component key={key}/>
}
The other way is to wrap the component in div
like this
import React from 'react';
export default [
<ComponentOne/>
<ComponentTwo/>
];
// Then wrap in div
{components.map((component, key) => (
<div key={key}>
{component}
</div>
}
the second way will not work, bc you cannot return something like this< <Component/>/>
– The Reason
Jan 6 '18 at 19:45
@TheReason And the reason?
– Prakash Sharma
Jan 6 '18 at 19:47
just updated a comment, you have to remove<
&/>
from elements of array component. Does it make sense?
– The Reason
Jan 6 '18 at 19:48
add a comment |
Actually you are exporting array of elements
from the file. One way is to export array of component and render them like
import React from 'react';
export default [
ComponentOne
ComponentTwo
];
// Then following will work
{components.map((Component, key) => (
// Remember to make first letter capital (in this case "c")
<Component key={key}/>
}
The other way is to wrap the component in div
like this
import React from 'react';
export default [
<ComponentOne/>
<ComponentTwo/>
];
// Then wrap in div
{components.map((component, key) => (
<div key={key}>
{component}
</div>
}
Actually you are exporting array of elements
from the file. One way is to export array of component and render them like
import React from 'react';
export default [
ComponentOne
ComponentTwo
];
// Then following will work
{components.map((Component, key) => (
// Remember to make first letter capital (in this case "c")
<Component key={key}/>
}
The other way is to wrap the component in div
like this
import React from 'react';
export default [
<ComponentOne/>
<ComponentTwo/>
];
// Then wrap in div
{components.map((component, key) => (
<div key={key}>
{component}
</div>
}
edited Jan 6 '18 at 19:49
answered Jan 6 '18 at 19:45
Prakash SharmaPrakash Sharma
6,92121120
6,92121120
the second way will not work, bc you cannot return something like this< <Component/>/>
– The Reason
Jan 6 '18 at 19:45
@TheReason And the reason?
– Prakash Sharma
Jan 6 '18 at 19:47
just updated a comment, you have to remove<
&/>
from elements of array component. Does it make sense?
– The Reason
Jan 6 '18 at 19:48
add a comment |
the second way will not work, bc you cannot return something like this< <Component/>/>
– The Reason
Jan 6 '18 at 19:45
@TheReason And the reason?
– Prakash Sharma
Jan 6 '18 at 19:47
just updated a comment, you have to remove<
&/>
from elements of array component. Does it make sense?
– The Reason
Jan 6 '18 at 19:48
the second way will not work, bc you cannot return something like this
< <Component/>/>
– The Reason
Jan 6 '18 at 19:45
the second way will not work, bc you cannot return something like this
< <Component/>/>
– The Reason
Jan 6 '18 at 19:45
@TheReason And the reason?
– Prakash Sharma
Jan 6 '18 at 19:47
@TheReason And the reason?
– Prakash Sharma
Jan 6 '18 at 19:47
just updated a comment, you have to remove
<
& />
from elements of array component. Does it make sense?– The Reason
Jan 6 '18 at 19:48
just updated a comment, you have to remove
<
& />
from elements of array component. Does it make sense?– The Reason
Jan 6 '18 at 19:48
add a comment |
Following up with my comment, you should be doing this instead:
{components.map((component, index) => (
<span key={index}>
{ component }
</span>
}
With React 16, you can use React.Fragment
:
{components.map((component, index) => (
<React.Fragment key={index}>
{ component }
</React.Fragment>
}
1
Rather than using a span, you could also useReact.Fragment
to prevent having to add a parent container: <React.Fragment key={index}>{component}</React.Fragment>
– Craig Myles
Nov 16 '18 at 0:08
add a comment |
Following up with my comment, you should be doing this instead:
{components.map((component, index) => (
<span key={index}>
{ component }
</span>
}
With React 16, you can use React.Fragment
:
{components.map((component, index) => (
<React.Fragment key={index}>
{ component }
</React.Fragment>
}
1
Rather than using a span, you could also useReact.Fragment
to prevent having to add a parent container: <React.Fragment key={index}>{component}</React.Fragment>
– Craig Myles
Nov 16 '18 at 0:08
add a comment |
Following up with my comment, you should be doing this instead:
{components.map((component, index) => (
<span key={index}>
{ component }
</span>
}
With React 16, you can use React.Fragment
:
{components.map((component, index) => (
<React.Fragment key={index}>
{ component }
</React.Fragment>
}
Following up with my comment, you should be doing this instead:
{components.map((component, index) => (
<span key={index}>
{ component }
</span>
}
With React 16, you can use React.Fragment
:
{components.map((component, index) => (
<React.Fragment key={index}>
{ component }
</React.Fragment>
}
edited Nov 16 '18 at 0:10
answered Jan 6 '18 at 19:50
John KennedyJohn Kennedy
2,72821227
2,72821227
1
Rather than using a span, you could also useReact.Fragment
to prevent having to add a parent container: <React.Fragment key={index}>{component}</React.Fragment>
– Craig Myles
Nov 16 '18 at 0:08
add a comment |
1
Rather than using a span, you could also useReact.Fragment
to prevent having to add a parent container: <React.Fragment key={index}>{component}</React.Fragment>
– Craig Myles
Nov 16 '18 at 0:08
1
1
Rather than using a span, you could also use
React.Fragment
to prevent having to add a parent container: <React.Fragment key={index}>{component}</React.Fragment>– Craig Myles
Nov 16 '18 at 0:08
Rather than using a span, you could also use
React.Fragment
to prevent having to add a parent container: <React.Fragment key={index}>{component}</React.Fragment>– Craig Myles
Nov 16 '18 at 0:08
add a comment |
All of these answers are almost right. Just remove the <../>
from your exports:
export default [
ComponentOne,
ComponentTwo,
]
And in the other file use .map()
:
export default class Dashboard extends React.Component {
render = () => (
<div className="tile is-parent">
{components.map((Component, key) => (<Component key={key} />))}
</div>
)
}
Also note that if you wanted to use Fragment
like others suggested you can just write <>...</>
instead.
add a comment |
All of these answers are almost right. Just remove the <../>
from your exports:
export default [
ComponentOne,
ComponentTwo,
]
And in the other file use .map()
:
export default class Dashboard extends React.Component {
render = () => (
<div className="tile is-parent">
{components.map((Component, key) => (<Component key={key} />))}
</div>
)
}
Also note that if you wanted to use Fragment
like others suggested you can just write <>...</>
instead.
add a comment |
All of these answers are almost right. Just remove the <../>
from your exports:
export default [
ComponentOne,
ComponentTwo,
]
And in the other file use .map()
:
export default class Dashboard extends React.Component {
render = () => (
<div className="tile is-parent">
{components.map((Component, key) => (<Component key={key} />))}
</div>
)
}
Also note that if you wanted to use Fragment
like others suggested you can just write <>...</>
instead.
All of these answers are almost right. Just remove the <../>
from your exports:
export default [
ComponentOne,
ComponentTwo,
]
And in the other file use .map()
:
export default class Dashboard extends React.Component {
render = () => (
<div className="tile is-parent">
{components.map((Component, key) => (<Component key={key} />))}
</div>
)
}
Also note that if you wanted to use Fragment
like others suggested you can just write <>...</>
instead.
answered Nov 16 '18 at 0:33
DustinDustin
16615
16615
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f48131100%2freact-render-array-of-components%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
You can't apply key to
component
as it will taken in as an attribute to the component.– John Kennedy
Jan 6 '18 at 19:41