I have the following component:
articles_list.jsx
import React from 'react';
import './articles_list.css';
export default class ArticlesList extends React.Component {
constructor(props) {
super(props);
this.state = {
articles: null
}
}
componentWillMount() {
fetch('/articles_all')
.then(res => res.json())
.then(json => {
this.setState({
articles: json.articles
});
});
}
render() {
var teste = () => {
if (this.state.articles === null) {
return(<div>No articles</div>)
} else {
{this.state.articles.forEach( function(element, index) {
console.log(element);
return <div key={index}>{element.title}</div>;
})}
}
}
return(
<div className="articles_list">
<div className="articles_list_title">
ARTICLES
</div>
<div>{teste()}</div>
</div>
);
}
}
Although the JSON request is working fine and returning an array with five JSON objects, they just don't render!
I am new to ReactJS and read (and watched) lots of tutorials, but it seems I am missing something.
Any suggestions?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…