score:178
You can map the list of stations to ReactElements.
With React >= 16, it is possible to return multiple elements from the same component without needing an extra html element wrapper. Since 16.2, there is a new syntax <> to create fragments. If this does not work or is not supported by your IDE, you can use <React.Fragment>
instead. Between 16.0 and 16.2, you can use a very simple polyfill for fragments.
Try the following
// Modern syntax >= React 16.2.0
const Test = ({stations}) => (
<>
{stations.map(station => (
<div key={station.call} className='station'>{station.call}</div>
))}
</>
);
// Modern syntax < React 16.2.0
// You need to wrap in an extra element like div here
const Test = ({stations}) => (
<div>
{stations.map(station => (
<div className="station" key={station.call}>{station.call}</div>
))}
</div>
);
// old syntax
var Test = React.createClass({
render: function() {
var stationComponents = this.props.stations.map(function(station) {
return <div className="station" key={station.call}>{station.call}</div>;
});
return <div>{stationComponents}</div>;
}
});
var stations = [
{call:'station one',frequency:'000'},
{call:'station two',frequency:'001'}
];
ReactDOM.render(
<div>
<Test stations={stations} />
</div>,
document.getElementById('container')
);
Don't forget the key
attribute!
score:2
There are couple of way which can be used.
const stations = [
{call:'station one',frequency:'000'},
{call:'station two',frequency:'001'}
];
const callList = stations.map(({call}) => call)
Solution 1
<p>{callList.join(', ')}</p>
Solution 2
<ol>
{ callList && callList.map(item => <li>{item}</li>) }
</ol>
Of course there are other ways also available.
score:2
This is quite likely the simplest way to achieve what you are looking for.
In order to use this map
function in this instance, we will have to pass a currentValue
(always-required) parameter, as well an index
(optional) parameter.
In the below example, station
is our currentValue
, and x
is our index
.
station
represents the current value of the object within the array as it is iterated over.
x
automatically increments; increasing by one each time a new object is mapped.
render () {
return (
<div>
{stations.map((station, x) => (
<div key={x}> {station} </div>
))}
</div>
);
}
What Thomas Valadez had answered, while it had provided the best/simplest method to render a component from an array of objects, it had failed to properly address the way in which you would assign a key during this process.
score:6
this.data
presumably contains all the data, so you would need to do something like this:
var stations = [];
var stationData = this.data.stations;
for (var i = 0; i < stationData.length; i++) {
stations.push(
<div key={stationData[i].call} className="station">
Call: {stationData[i].call}, Freq: {stationData[i].frequency}
</div>
)
}
render() {
return (
<div className="stations">{stations}</div>
)
}
Or you can use map
and arrow functions if you're using ES6:
const stations = this.data.stations.map(station =>
<div key={station.call} className="station">
Call: {station.call}, Freq: {station.frequency}
</div>
);
score:59
I have an answer that might be a bit less confusing for newbies like myself. You can just use map
within the components render method.
render () {
return (
<div>
{stations.map(station => <div key={station}> {station} </div>)}
</div>
);
}
Source: stackoverflow.com
Related Query
- Rendering React Components from Array of Objects
- React.js: Rendering Components from Array of Objects
- Dynamically rendering react components from string array
- Render array of objects from Redux store as React components
- Components are not rendering while using .map() on array of objects in React
- React not rendering components from an array
- How to remove duplicate values from an array of objects in react for select components of MUI
- React JS: how to properly remove an item from this.state.data where data is an array of objects
- Get first object from array of objects in react
- Rendering React Components from an Object
- Rendering React components from json data dynamically
- Render React components from array
- get all values from array of objects in react typescript
- Remove element of an object from array of objects - react state
- Taking React State (object of objects(example inside)) and getting an array of objects to map() for component rendering
- How to delete objects from react state hook array with a button click
- Rendering React components from text
- Creating an array in React with objects created from two lists
- Rendering random elements from an array in React
- Error rendering item from state array in React
- Mirroring the position when moving objects from the array to the array to another object in React
- Return multiple HOC components from Array in React JS
- get lat and lng from array of objects on React
- ReactDOM dynamically rendering components from array
- React rendering before array is populated from GET Request
- React Hooks - Preventing child components from rendering
- Rendering an array of components with seperate states in React
- Rendering React components mapped from Firebase Firestore
- return array of objects from props React JS
- Rendering an array of objects React
More Query from same tag
- Updating input value by updating props
- React.memo not working with functoinal components and google maps
- Can't update an object in Redux without overwriting it or losing other items in state
- How to jest.mock an ES6 module import?
- Best way to integrate webpack builds with ASP.NET Core 3.0?
- How to write clean, readable ReactJS Components with conditional renders?
- When Header is added the Protected Route is not getting rendered
- React - Redux - Connect data fetched to Component
- Best way to track values of multiple text inputs to confirm they are all correct in a JavaScript/React word game
- How to render jsx correctly
- filter array doesn't work in React component
- How to pass props to DatePicker in NextJS functional Component
- Grabbing the Value of a Dynamically Created Object
- Why nothing happens on onClick in my component?
- Mobile toolbar/menu hides full website height
- What is the Flux Standard Action convention for structuring a payload?
- How can you trigger a rerender of a React js component every minute?
- prop doesn't update when state changes
- Default property value in React component with extended props using TypeScript 3
- Storybook install with react
- ReactJS rendering data that is passed from a laravel controller as json string
- rsuite-table not updating when state data is changed
- How to add hash to urls in react-router 4
- Proxy API request using React Router
- Mock keyboard event and test stopImmediatePropagation is called with React
- How can i justifyContent only on specific sizes in react material ui?
- Cannot read property setState of undefined
- How to attach file using Nodemailer in React.js
- State awaiting in React - I'm having trouble understanding the error while using 'useCallback()'
- React index key is not working in the map