You should use component instead of string in item.type like this
import Foo from './Foo';
import Bar from './Bar';
[ { type: Foo, }, { type: Bar, }, { type: Baz}]
UPDATE:
If you do not have component reference in advance then use a mapping object which convert your string to component reference like this
import Foo from './Foo';
import Bar from './Bar';
const mapper = {
Foo: Foo,
Bar: Bar,
}
// Then use it like this
const getItems = this.props.itemslist.map((item, key) => {
const Type = mapper[item.type];
rendered.push(<Type data-attributes={attributes} key={key} />);
});
Answer from Prakash Sharma on Stack OverflowYou should use component instead of string in item.type like this
import Foo from './Foo';
import Bar from './Bar';
[ { type: Foo, }, { type: Bar, }, { type: Baz}]
UPDATE:
If you do not have component reference in advance then use a mapping object which convert your string to component reference like this
import Foo from './Foo';
import Bar from './Bar';
const mapper = {
Foo: Foo,
Bar: Bar,
}
// Then use it like this
const getItems = this.props.itemslist.map((item, key) => {
const Type = mapper[item.type];
rendered.push(<Type data-attributes={attributes} key={key} />);
});
The first mistake is see if an incorrect use of .map. Remember that .map traverses each array element and changes them. Right now, you are using it as if it were .forEach.
Your code should look more like this:
const getItems = this.props.itemslist.map((item, key) => {
const TYPE = item.type;
return <TYPE data-attributes={attributes} key={key} />
});
How to render a JSX component for each item in array
How can I render an array of jsx elements in ReactJs
Render Array of JSX element inside Helper function
reactjs - React render array of components - Stack Overflow
How to render an array of objects in React?
Step 1: Create a react application.
Step 2: Change directory.
Step 3: Create data as an array.
Step 4: Mapping the array into a new array of JSX nodes as arrayDataItems.
Step 5: Return arrayDataItems from the component wrapped in
How do you render multiple objects in React?
How do you set an array of objects in state in React JS?
To set an array of objects in the state of a React component, you can use the 'useState' hook. So to do this, first, import 'useState' from 'react'. Then, declare a state variable using useState and initialize it with your array of objects. To update the state, use the setter function provided by the useState hook. And voila, now you can easily manage and modify the array of objects within your component's state.
Videos
You need to render a list of components in render() function.
Not render multiple times for each album.
Here is how you might render a list of album artists.
1. Declare the state to hold albums (an empty array by default) retrieved.
2. On handleSearchClick, retrieve albums and set the albums state.
3. In render(), if album is not found, then display appropriate messages.
4. else create a list of artist components (wrapped in <li>).
5. Display the component by returning it.
WARNING: this is not a production/clean code.
class SearchResult extends React.Component {
state = { albums: [] };
searchAlbums = async (searchValue) => (await spotifyWebApi.searchAlbums(searchValue));
handleSearchClick = async (e) => {
const searchValue = e.target.value;
const {items: albums} = await this.searchAlbums(searchValue);
this.setState({albums});
}
render() {
const {albums} = this.state;
if (!album) return <div>loading...</div>;
if (album.length === 0) return <div>No albums found</div>;
// Generate artists components.
const albumsComponents = albums.map(album =>
album.artists.map(artists => (<li key={artists.name}>{artists.name}</li>)
}
return (
<div>
Search Term:
<input
value={this.state.searchTerm}
onClick={this.handleSearchClick} />
{albumsComponents}
</div>
);
}
}
For now you're doing it in reverse. You have to map albums array inside of render function, not iside of a searchAlbums. What you can do, is to place albums array inside of state:
constructor(){
super();
this.state = {
albums: []
}
}
and in searchAlbums function set it in that state:
searchAlbums(){
spotifyWebApi.searchAlbums(this.state.value)
.then((response) => {
this.setState({albums: response.albums.items});
});
}
after that, in render function map it, like you did:
render(){
return this.state.albums.map((t) => {
return t.artists.map((artistsArray, index) => {
return (
<div>
<li key={artistsArray.name}>
{artistsArray.name}
</li>
</div>
)
});
});
}
To render an array of JSX elements, you should be able to do this:
renderCell = (row) => {
return row.map(cell => {
return <TableCell key={i} align="right">{ row[prop] }</TableCell>
}
}
Edit:
<TableBody>
{(pagination
? rows.slice(page * rowsPerPage, page * rowsPerPage + rowsPerPage)
: rows
).map((row) => {
return (
<TableRow
onClick={onRowClicked(row)}
hover
role="checkbox"
tabIndex={-1}
key={row[props.rowsUniqueKey]}
>
{columns.map((column) => {
const value = row[column.id];
return (
<>
<TableCell
key={column[props.columnsUniqueKey]}
align={column.align}
>
{column.format && typeof value === 'number'
? column.format(value)
: value}
</TableCell>
</>
);
})}
{/* { enableActions && <TableCell> Actions </TableCell> } */}
</TableRow>
);
})}
</TableBody>
from: Create dynamic action column in React Material UI Table
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' });
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
Have you ever used anything rather then .map to render list in JSX?