I encountered an issue when I used map function in React to iterate data because the data is object. So I tried to use _.map function of Lodash to convert from object to array. Then I want to iterate the data using mapfunction.
It didn't work as I had expected and I'm wondering what is the problem.
Should it be working? It is a ok to use _.map to iterate in React?
The code
import React, { Component } from 'react';
import { connect } from "react-redux";
import { bindActionCreators } from "redux";
import { addData } from "../actions/index";
import _ from "lodash";
class List extends Component{
componentDidMount(){
this.props.addData()
}
render(){
let item;
if(this.props.data) {
item = _.map(this.props.data, data => {
return (
<li key={data.id}>
{data.title}
</li>
/* this returns empty data. Only bullet button appears.
In console, it says [Each child in an array or iterator should have a unique "key" prop.]
Even though I added a key value. */
)
})
}
return(
<div>{item}</div>
)
}
}
function mapStateToProps(state){
return {
data : state.data
}
}
export default connect(mapStateToProps, {addData})(Search);
UPDATE:
To debug, did a console.log(this.props.data), which produced this output:
Object { data: Array[100] }

redux thunkto get data asynchronously.