Once again, I'm facing a very common mistake "Cannot read property 'map' of undefined". I have a initial list of items (movies) loading properly inside a <ul><li></li></ul>. However after dispatching my action by clicking my button, the error happen. I can actually see the state updated in my redux dev tool (even with the error) but I also can see that the change doesn't impact my store state in my react dev tool.
So here is my reducer :
export default function moviesReducer(state = {items}, action) {
if(action.type === 'SET_MOVIES_FILTER') {
return update(state.items, {$splice: [[1,3]]})
}
return state;
}
Here is my item list (from here post) :
let items = [{
title: 'Mad max',
year: 2015,
rating: 8,
genre: 'fantasy',
}, {
title: 'Spider man 2',
year: 2014,
rating: 7,
genre: 'fantasy',
}, {
title: 'Iron man 3',
year: 2013,
rating: 7,
genre: 'fantasy',
}, {
title: 'Dumb and Dumber To',
year: 2014,
rating: 5,
genre: 'comedy',
}, {
title: 'Ted 2',
year: 2015,
rating: 6,
genre: 'comedy',
}];
Here is my action :
export const SetMoviesFilter = () => {
return {
type: 'SET_MOVIES_FILTER'
};
};
And here is my components :
const movieTable = ({testmovie, movieTable }) => {
return (
<div>
<button onClick={movieTable}>testbutton</button>
<ul>
{testmovie.map((m, i) =>
<li key={i}>{m.year} - {m.title}.({m.genre}) - {m.rating}</li>
)}
</ul>
</div> )
}
function mapStateToProps(state) {
return {
testmovie: state.moviesReducer.items
};
};
const mapDispachToProps = (dispatch) => {
return {
movieTable: () => {dispatch (SetMoviesFilter());
},
};
};
const AppMovie = connect(mapStateToProps, mapDispachToProps)(movieTable);
Where am I wrong ? what should I do ? I don't know if it's related but I can't make my jsbin work.
thanks.
debugger;on the first like of your movieTable function and it will pause on that line when you have the dev tools open in chrome.<ul> {testmovie.map((m, i) => <li key={i}>{m.year} - {m.title}.({m.genre}) - {m.rating}</li> )} </ul>I only have the 'map' of undefined error when clicking on the button.