javascript - 反应状态未设置为仅对象中的一个键
问题描述
我真的不知道如何解释这一点。
我的网站上有一个个人资料页面 ( /profile
),它访问 API ( /api/testAPI/currentuser.php
)
currentuser.php
返回以下 JSON -{loggedin: true, uid: "3", username: "TechPhil", permissions: "none"}
在我的反应组件中,我componentDidMount()
使用以下函数在阶段中获取此端点
fetch("/api/testAPI/currentuser.php")
.then((res) => res.json())
.then((res) => {
this.setState({
apiResponse: res
})
})
不要问我为什么,但是我可以使用uid
,username
和permissions
键很好地访问this.state.apiResponse.key
,但是由于某种原因我根本无法访问loggedin
?
我已将完整的组件代码放在下面。
import React, {Component} from 'react'
import {Redirect, Link} from 'react-router-dom'
class ProfilePage extends Component {
constructor(props) {
super(props);
this.state = {apiResponse:{}};
}
getUser(){
fetch("/api/testAPI/currentuser.php")
.then((res) => res.json())
.then((res) => {
this.setState({
apiResponse: res
})
})
}
componentDidMount() {
this.getUser();
}
render() {
if (this.state.apiResponse.loggedin){
return(
<div className="container">
<h2>Your Profile</h2>
<p>User ID: {this.state.apiResponse.uid}</p>
<p>Username: {this.state.apiResponse.username}</p>
<p>Permissions: {this.state.apiResponse.permissions}</p>
<Link to="/profile/changepw">Change your Password</Link>
</div>
)
} else {
return(<Redirect to="/login"/>)
}
}
}
export default ProfilePage
解决方案
推荐阅读
- macos - 将 QueryInterface 从 stdcall 更改为 register
- r - 在 macOS Catalina 上安装 RcppArmadillo 时出错
- python - pymodbus:更新运行服务器的上下文
- c++ - 定义易于维护的函数入口退出记录器时如何避免使用宏
- html - 为什么按钮不显示回来?
- excel - 通过 vba 从网站下拉列表中选择值
- javascript - 上传按钮 C# 在上传文件之前检查 MD5 哈希
- c++ - libuv - 空闲的写缓冲区
- html - 无法使用 CSS 创建页脚
- python - AutoML Vision Edge 中的 saved_model 未正确加载