【发布时间】:2026-02-03 06:15:01
【问题描述】:
我试图在获取请求后使用以下数据更改我的状态,但在设置状态后状态永远不会改变
profileinfo:
{
Firstname:'Jeff ',
Lastname:'Series',
email:'exam.com',
Address:'502B Inner Circle Riverwalk'
}
componentDidMount() {
fetch("http://localhost:3001/login")
.then(response=>response.json()
.then(data=>(
this.setState({profileinfo:data[0].firstname}))))
console.log(this.state.profileinfo)
}
【问题讨论】:
-
为了
setState()工作,您需要将整个状态存储在一个名为 state 的对象上,您正在尝试更新一个不存在的状态