【发布时间】:2020-10-20 07:06:35
【问题描述】:
我正在尝试对我的 db.json 的最后一个元素使用 axios 补丁请求。 为此,我将“id”的状态设置为来自 json 的数据的长度。如果我控制台记录我的状态,它会给我正确的数字。但是,如果我将状态放到 URL 中,那里什么也不会发生。使用普通数字而不是状态是有效的。我做错了什么?
handleSubmit = event => {
const sub = this.newsletter();
axios.get('http://localhost:3001/posts')
.then(res => {
this.setState({id: res.data.length});
console.log(this.state.id);
});
if(sub){
axios.patch(`http://localhost:3001/posts/${this.state.id}`,{
newsletter: 'yes'
})
.then(res => {
console.log(res);
console.log(res.data);
})
}
}
【问题讨论】: