你好,由于某种原因,React 不会全局更新状态,
componentDidMount() {
var id = window.location.href.split('/')[3]
var xhr = new XMLHttpRequest();
xhr.open("GET", "https://reactAppBackend/edit/" + id);
xhr.send();
xhr.onloadend = () => {
this.setState({singlePost: JSON.parse(xhr.responseText)})
console.log(this.state.singlePost[0])
};
}
当我运行这个控制台日志返回数据这个数据
{_id: "5e6016adb8c32b00883f55f5", postTitle: "RE4", postContent: "the best ", __v: 0}
但是当我尝试渲染状态时:
render() {
return (
<div>{this.state.singlePost[0]}</div>
)
}
它没有渲染任何东西。