React native 如何在我的本机应用程序中检索获取数据
我想知道如何在我的应用程序中查看fetch查询中的数据 我有一个从React native获取的节点,我想显示响应 节点部分React native 如何在我的本机应用程序中检索获取数据,react-native,React Native,我想知道如何在我的应用程序中查看fetch查询中的数据 我有一个从React native获取的节点,我想显示响应 节点部分 app.get('/balance', function(req, res){ //calling the function res.jsonp('0'); console.log("CRYPTO CALLED"); }); 反应功能 _getBalanceFromApiAsync() { fetch('http://192.1
app.get('/balance', function(req, res){
//calling the function
res.jsonp('0');
console.log("CRYPTO CALLED");
});
反应功能
_getBalanceFromApiAsync() {
fetch('http://192.168.1.100:3000/ballance')
.then(response => response.json())
.then(responseJson => {
console.log(responseJson);
return responseJson;
})
.catch((error) => {
console.error(error);
});
}
我可以在节点控制台和react控制台中看到结果。但它不起作用的地方就在这里
本地应用程序
<Text style={styles.getStartedText}>Your Wallet Balance</Text>
<Text> {this._getBalanceFromApiAsync()}</Text>
</View>
您的钱包余额
{this.\u getBalanceFromApiAsync()}
函数正在执行,但我想显示返回值,因此文本字段保持为空
谢谢很简单,您需要设置状态以重新呈现组件。试着这样做
constructor(props){
super(props)
this.state = {
textData: ''
}
}
componentDidMount(){
this.getBalanceFromApiAsync()
}
getBalanceFromApiAsync() {
fetch('http://192.168.1.100:3000/ballance')
.then(response => response.json())
.then(responseJson => {
console.log(responseJson);
this.setState({
textData: responseJson
})
})
.catch((error) => {
console.error(error);
});
}
<Text style={styles.getStartedText}>Your Wallet Balance</Text>
<Text> {this.state.textData}</Text>
</View>
构造函数(道具){
超级(道具)
此.state={
文本数据:“”
}
}
componentDidMount(){
此文件为.getBalanceFromApiaAsync()
}
getBalanceFromApiAsync(){
取('http://192.168.1.100:3000/ballance')
.then(response=>response.json())
.然后(responseJson=>{
console.log(responseJson);
这是我的国家({
textData:responseJson
})
})
.catch((错误)=>{
控制台错误(error);
});
}
你的钱包余额
{this.state.textData}
您想显示哪些具体数据?您的函数似乎是将对象返回到要显示的文本字段中。对象无法显示在react本机文本标记中。在这种情况下,我试图显示一个整数0我正在尝试,因此不确定我是否正确。尝试返回一个对象{balance:0}并引用responseJson.balance,但我得到了相同的结果。在函数中,是否只返回responseJson.balance?