React native React Native-setState获取响应(不设置并停止代码执行)
我正在尝试将应用程序组件中的React native React Native-setState获取响应(不设置并停止代码执行),react-native,React Native,我正在尝试将应用程序组件中的用户:[]设置为获取的响应 当我尝试使用this.setState更改用户:的值时,我的代码停止执行,而this.state.users保持为空 不太清楚发生了什么,我是否正确使用了组件willmount?(有人在教程中使用了它,我对生命周期方法的正确使用有点困惑) 我对这种反应不熟悉 此外,由于“世博会”没有回应我像疯子一样摇晃手机,我无法查看错误日志 import React, {Component} from 'react'; import { StyleShe
用户:[]
设置为获取的响应
当我尝试使用this.setState
更改用户:
的值时,我的代码停止执行,而this.state.users
保持为空
不太清楚发生了什么,我是否正确使用了组件willmount
?(有人在教程中使用了它,我对生命周期方法的正确使用有点困惑)
我对这种反应不熟悉
此外,由于“世博会”没有回应我像疯子一样摇晃手机,我无法查看错误日志
import React, {Component} from 'react';
import { StyleSheet, Text, View } from 'react-native';
export default class App extends React.Component {
constructor() {
super();
this.state = {
users: [],
}
}
componentWillMount() {
// Not sure if I'm using this ^ correctly
this._fetchUsers();
}
render() {
const users = this._getUsers();
return (
<View style={styles.container}>
<Text>Users</Text>
<View>
{users}
</View>
</View>
);
}
_fetchUsers() {
fetch('http://api.example.111.111.11.111.xip.io/users')
// This ^ is returning json
.then((response) => {
console.log(1)
// This ^ log is visible
this.setState({users: response._bodyText});
// Code stops executing around here
console.log(2)
// This ^ log is not visible
})
}
_getUsers() {
return this.state.users.map((user) => {
return(<User
name={user.name}
email={user.email}
key={user.id} />);
})
}
}
class User extends React.Component {
render() {
return(
<View className="user">
<Text className="user-name">{this.props.name}</Text>
<Text className="user-email">{this.props.email}</Text>
</View>
)
}
}
const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: '#fff',
alignItems: 'center',
justifyContent: 'center',
}
});
所以使用响应。_bodyText
给了我一个json字符串,这解释了为什么它不能作为对象工作
根据@Srdjan Cosic的建议
response.json()
给了我:
Promise {
13:54:17: "_40": 0,
13:54:17: "_55": null,
13:54:17: "_65": 0,
13:54:17: "_72": null,
13:54:17: }
我还没有涵盖承诺,所以我根本不明白这一点
和响应。数据给了我未定义的
最后,这个.setState({users:JSON.parse(response._bodyText)})的工作原理是什么?
,这也是@Srdjan Cosic的部分建议
奇怪的是,this.setState({users:JSON.parse(response)})
返回了完全相同的结果,但仍然不起作用:/
TL;DR-我已经开始使用componentDidMount(感谢大家的建议)
,我的json响应很奇怪,所以不得不使用this.setState({users:json.parse(response.\u bodyText)})
将json的一部分转换为对象
谢谢大家的帮助。不要在componentWillMount()中使用副作用(异步调用,如api调用、获取数据或设置状态),因为它是在首次呈现或装载组件之前使用的。在渲染发生之前,获取数据的异步调用不会返回。因此,将此._fetchUsers()放入componentDidMount()中
让我知道它是否有效)首先,根据componentWillMount()
上面说
componentWillMount()
在装载发生之前被调用。它在render()
之前调用,因此在此方法中同步调用setState()
不会触发额外的渲染。通常,我们建议使用构造函数()
避免在这种方法中引入任何副作用或订阅。对于这些用例,请改用componentDidMount()
这基本上意味着您应该尝试在构造函数()或组件安装()中调用。\u fetchUsers()
第二件事,我不确定什么是响应。_bodyText
是因为我在Fetch文档中找不到任何关于它的信息,但是如果响应对象是Fetch文档中描述的对象,那么您可以访问.body
属性。因此,与其这样做
this.setState({users:response.\u bodyText})
为什么不试试this.setState({users:JSON.parse(response.body)})
这将解析主体(假设它是您描述的JSON格式)并将其转换为javascript对象
我使用axios,如果我的响应返回一个json,我通常可以使用response.data
访问它,这样也可以用于fetch。否则还有response.json()
在响应转换为json后返回另一个承诺您必须在使用它之前将响应转换为json
async _fetchUsers() {
let response = await fetch('http://api.example.111.111.11.111.xip.io/users');
let responseJson = await response.json();
return responseJson.movies;
})
}
componentWillMount() {
let users = this._fetchUsers();
this.setState({ users });
}
现在看起来是这样的:componentDidMount(){this.\u fetchUsers();}不幸的是,这会导致完全相同的结果,但是感谢您的建议:)@WilliamOsborne哦,您正在使用fetch。Fetch返回http响应,而不是实际的json。因此您必须获取(')。然后((res)=>res.json())。然后((response)=>console.log(response))////请参阅调试器中的console.log output以查看响应内容,并相应地设置数据。大多数情况下,它是在response.data.等数据中,而不是确切的答案(更多详细信息,请参阅更新后的内容),但它让我与我已经拥有的最接近。为了方便起见,我们将这个答案标记为已解决,ThanksOk,是的,在没有错误输出的情况下理解它有点困难,但我很高兴它让您进入了正确的方向。用response.json()
回答您的问题是,这是一个承诺。因此,您可以这样编写fetch(“一个url.com”)。然后(response=>response.json())。然后(response=>this.setState({users:response})
注意:您可能还需要在那里执行json.parse()。无论如何,很高兴它让您有所收获:)
async _fetchUsers() {
let response = await fetch('http://api.example.111.111.11.111.xip.io/users');
let responseJson = await response.json();
return responseJson.movies;
})
}
componentWillMount() {
let users = this._fetchUsers();
this.setState({ users });
}