【发布时间】:2018-11-18 19:42:46
【问题描述】:
在本机反应中,我从键中获取了值,但无法从先前的值中获取值。
例如我有以下 json
{
"username":"xyz",
"xyz":{"role":3,"role_name":"abcd"}
}
我需要获取用户名并使用用户名值需要访问角色。我试过跟随。
constructor(){
this.state={rol:'role',uname:'',xtext:'',}
}
fetch('example.com/url')
.then(response => response.json())
.then(responseobj => {
this.setState({
uname: responseobj.username,
xtest: responseobj[this.state.uname][this.state.rol],
});
我得到了用户名,但没有得到角色。
【问题讨论】:
-
显示
fetch的使用上下文。它在方法中吗?毫无疑问,constructor在class之内,你能显示更多class吗?
标签: javascript json react-native fetch-api