【发布时间】:2019-08-09 20:15:17
【问题描述】:
我尝试了多种设置状态的方法,但由于某种原因,状态永远不会更新。这是我希望状态更改为的 JSON 数据
export class Provider extends Component {
state = {
posts: [],
profileinfo: {},
dispatch: action => this.setState(state => reducer(state, action))
};
componentDidMount() {
fetch("http://localhost:3001/login").then(response =>
response
.json()
.then(data => this.setState({ profileinfo: data.firstname }))
);
console.log(this.state.profileinfo);
}
render() {
// ...
}
}
【问题讨论】:
-
请以文本格式包含数据,而不是图像。
-
您必须将您的 componentDidMount() 设置为比较旧状态/道具与新状态/道具的条件。那么你将避免无限循环
-
承诺得到解决?
-
不,它没有得到解决