I'm requesting a sample JSON array from http://jsonplaceholder.typicode.com/users and creating a list of items using the name values of that array in a ReactJS project.
The list is a Grommet list and I use the .map function to map the web service data to each list item.
But when the code renders, instead of creating a list of names using the web service. It creates only one list item and renders the call to .map and rows.push.
Question:
How can you to map a JSON array to a List in ReactJS?
This is what I've tried, first getting the web service via an AJAX GET and then calling .map to map that data to a list:
loadNameData() {
$.ajax({
url: 'http://jsonplaceholder.typicode.com/users',
dataType: 'json',
success: function(data) {
this.setState({data: data});
}.bind(this),
error: function(xhr, status, err) {
console.error('#GET Error', status, err.toString());
}.bind(this)
});
}
getInitialState(){
return {
data: {
names: []
}
};
}
componentDidMount() {
this.loadAssetData();
}
render() {
let layerNode;
var rows = [];
layerNode = (
<Layer flush={true} closer={true} onClose={() => this._onCloseLayer()} align='right'>
<Box pad='medium' colorIndex='grey-3'>
<Heading>Filter Options</Heading>
<Label>Names</Label>
this.props.data.names.map(function(name) {
rows.push(<ListItem justify="between">)
rows.push(<span>)
rows.push({name})
rows.push(</span>)
rows.push(</ListItem>)
}
<List selectable={true} selected={0}>
{rows}
</List>
</Box>
</Layer>
);
return (
<Section pad="small" full="vertical" flex={true}>
{layerNode}
</Section>
);
}

.map()works, right? The anonymous function you're passing to it doesn't explicitly return anything, which means thatundefinedis returned every time. So your.map()is returning an array ofundefinedvalues.mapmethod and I see now that I need to pass in the item to map and in turn return that item. But the following gives a syntax error on my return statement. Any idea why that is? hastebin.com/ewebohocom.js<ListItem />", right? So you should be returning the<ListItem />instead.