这种东西很好地解释了
文档
。
每次执行API请求时都需要使用state,因为它是一种副作用。我建议您阅读文档并理解它,但要使其在您的组件中工作,请在组件的开头添加:
class BlaBla extends Component {
state = {
response: undefined
};
/* Rest of component code */
}
</code>
并将您的获取请求更改为如下所示:
fetch(‘/‘, { method: ‘POST’ }).then(response => this.setState({response}));
</code>
通过添加状态,您也会遇到绑定问题,因此将方法声明更改为箭头函数,由此:
handleSubmit(event) { / code / }
</code>
对此:
handleSubmit = (event) => { / code / }
</code>
要在答案段落中显示结果,请执行以下操作:
Answer: {this.state.response}
</code>