Javascript 我在渲染中获得了我的函数,但我可以';我看不出来
我使用fetch获取数据(它正在工作)-我将它放在我的componentdidmount上,然后编写函数来呈现数据 我可以在控制台上看到它,但它没有出现在我的屏幕上: 还有我的代码:Javascript 我在渲染中获得了我的函数,但我可以';我看不出来,javascript,arrays,reactjs,Javascript,Arrays,Reactjs,我使用fetch获取数据(它正在工作)-我将它放在我的componentdidmount上,然后编写函数来呈现数据 我可以在控制台上看到它,但它没有出现在我的屏幕上: 还有我的代码: getMesCommentaires = () => { console.log("coucou"); const headers = new Headers({ "Content-Type": "application/json
getMesCommentaires = () => {
console.log("coucou");
const headers = new Headers({
"Content-Type": "application/json",
"X-Requested-With": "XMLHttpRequest",
Authorization: "bearer " + localStorage.getItem("token"),
});
const options = {
method: "GET",
headers: headers,
};
fetch("http://localhost:8080/serveur/mesCommentaires", options)
.then((response) => {
return response.json();
})
.then(
(responseObject) => {
const mesCommentaires = responseObject;
if (Array.isArray(mesCommentaires)) {
this.setState({ commentaires: mesCommentaires });
}
console.log(this.state);
},
(error) => {
console.log(error);
}
);
};
我的职能:
renderMesCommentaires = () => {
return this.state.commentaires.map((element, index) => {
return (
<div key={index}>
<h1 className="statut">{element.nom}</h1>
<h1 className="statut">{element.prenom}</h1>
<h1 className="statut">{element.texte}</h1>
</div>
);
});
};
componentDidMount(){
this.getMesCommentaires();
};
renderMesCommentaires=()=>{
返回this.state.commentaries.map((元素,索引)=>{
返回(
{element.nom}
{element.prenom}
{element.texte}
);
});
};
componentDidMount(){
这个.getmescommentals();
};
状态似乎不是您期望的状态-
注释
是一个数组,但值(可能)未定义。尝试在renderMesCommentaires
的开头添加console.log(this.state.commentaries)
,以验证您是否具有所需的值。要渲染某事物,需要使用渲染方法。尝试将renderMesCommentaires更改为render。@Kryten当我将console.log放在rendermescommentaires@jakubJanik我的渲染上有渲染注释:)