【发布时间】:2020-10-15 22:10:45
【问题描述】:
我很难找到这个问题的答案。假设我有以下脚本(不起作用):
class ProfileCard extends React.Component {
constructor(props) {
super(props);
this.state = {
error: null,
isLoaded: false,
items: {},
};
}
componentDidMount() {
fetch("/accounts/api/" + username)
.then(res => res.json())
.then(
(result) => {
this.setState({
isLoaded: true,
items: result
});
},
(error) => {
this.setState({
isLoaded: true,
error
});
}
)
}
}
这分别是result 和this.state.items 的样子:
{last_login: "2020-06-25T09:50:24.218Z", is_superuser: "true", is_staff: "true", …}
{}
如何使this.state.items 与result 具有完全相同的内容?请假设所有使用的变量和方法都已声明。
【问题讨论】:
-
你所拥有的对我来说看起来不错。也许您正试图在数据可用之前访问
this.state.items?请提供一个完整的例子。
标签: reactjs dictionary ecmascript-6 jsx