【发布时间】:2018-09-19 05:39:22
【问题描述】:
import React from 'react';
import ReactDOM from 'react-dom';
import $ from 'jquery';
import Post from './components/Post.jsx';
import Feed from './components/Feed.jsx';
class App extends React.Component {
constructor() {
super();
this.state = {
view: 'feed',
collection: ''
}
}
componentDidMount() {
console.log('component did mount')
this.getCollection();
}
async getCollection() {
try {
const response = await fetch('/api/page');
const responseJSON = await response.json();
this.setState({ collection: responseJSON }, () => {
console.log("App Component - getCollection() State Updated", this.state);
});
} catch (error) {
console.log("App Component - getCollection() error", error);
}
}
render() {
return (
<div>
Text
</div>
);
}
}
ReactDOM.render(<App />, document.getElementById('app'));
无法让组件挂载到功能。尝试向我的 mongodb 发出 ajax 请求并在客户端呈现该请求。我现在需要做的就是进行状态更改,将集合设置为我返回的信息。我尝试使用 Ajax 请求,但没有奏效。现在我正在根据另一位贡献者的建议实现异步获取调用。
非工作 ajax 请求:
截至目前,componentDidMount仍未被触发,状态的collection属性仍为空字符串。
【问题讨论】:
-
你必须绑定
getCollection -
我绑定了成功和错误的情况,我还需要在哪里?我无法触发成功控制台日志案例
-
你需要添加一个
render函数 -
你能把整个文件的代码加进去吗?
标签: reactjs