I have tried the solution in the link below but still can't figure it out. I'm a newbie in react and also JS. Can anyone help me out. @@
Cannot read property 'map' of undefined
const mapStateToProps = state =>{
return {
data:{
names: state.names,
ages: state.ages,
genders: state.genders
},
};
}; // This is the copy of array in Redux state, it comes from the reducer.
const ConnectedList = ({data})=>(
<ul>
{data.names.map(el => (
<li key={el.id}>{el.data.names}</li>
))}
{data.ages.map(el => (
<li key={el.id}>{el.data.ages}</li>
))}
{data.genders.map(el => (
<li key={el.id}>{el.data.genders}</li>
))}
</ul>
);
const List = connect(mapStateToProps)(ConnectedList);
export default List;
Here is the reducer
const initialState = {
data:{
names:[],
ages:[],
genders:[],
}
};
function rootReducer (state = initialState, action){
if(action.type === ADD_DATA){
return Object.assign({}, state, {
names: state.data.names.concat(action.payload),
ages: state.data.ages.concat(action.payload),
genders: state.data.genders.concat(action.payload)
});
}