I'm trying to show a list of articles in my component and I'm getting "Cannot read property 'map' of undefined" error on my "articleList.js" component .what's wrong with my code ? and also When I log articles , It's undefined.
I have below Code in my reducer :
const initialState = {
articles: [],
loading: false,
error: null
};
export default (state=initialState, action) => {
switch (action.type) {
case GET_ARTICLES:
return {
...state,
articles: action.payload,
loading: false
};
code for action :
export const getArticles =()=> dispatch =>{
fetch('http://localhost:8000/api/articles/')
.then(handleErrors)
.then(res => dispatch(
{
type : GET_ARTICLES,
payload: res.json
}
))}
code for component :
componentDidMount() {
this.props.getArticles();
}
render() {
const { articles } = this.props.article;
return (
<div>
<div>
<div>
{articles.map(({ id, header }) => (
<div key={id} timeout={500} className="fade">
<div>
<button className="remove-btn" onClick={this.onDeleteClick.bind(this, id)} >Remove</button>
{header}
</div>
</div>
))}
</div>
</div>
</div>
);
}
}
const mapStateToProps = state => ({
article: state.article
});
export default connect(
mapStateToProps, { getArticles} )(ArticleList);
Sorry about the large amount of code dumped, its just all related and I believe the error lies somewhere within having an undefined initialState but I cant see what I have missed defining
articles, your payload reads it fromarticleand your statemapper gets it fromarticle, so which one is correct now? And your render gets it fromthis.props.article.articles? How is your data looking?res.json()instead of justres.jsonor is the handleErrors before that doing something we don't see? :)