我的问题与React Native fetch() Network Request Failed 不同,我并没有在 http 或 https 上挣扎,如果请求由于互联网连接、错误的 API 等而失败,我只想向用户提供一个很好的错误。得到反应本机错误。
我有一个表格,想把它发送到服务器并得到回复,所以我写了这个:
submitForm = async () => {
fetch("www.somewhere.com", {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify(some_data)
})
.then((response) => response.json())
.then((responseJson) => {
// do something
})
.catch((error) => {
this.setState({server_error: "request failed try again."});
});
};
Run Code Online (Sandbox Code Playgroud)
但似乎我的捕获无法正常工作,因为如果请求失败,我会从 react native 收到如下错误:
而在生产中,它只是跳出应用程序,我该如何避免这种情况?
我有一个后退按钮,可让用户返回屏幕,但是当没有屏幕可返回时,我希望它做其他事情,所以这是我的代码:
<Button onPress={()=>{
if(CanGoBack){ // imaginary 'CanGoBack' variable
this.props.navigation.goBack()
}else{
this.doSomething()
}
}}/>
Run Code Online (Sandbox Code Playgroud)
我该如何实现?
根据世博会文件:
为应用程序提供图标的最直接方法是在 app.json 中提供图标键
所以我将自己的图标添加到expo默认图标所在的资产文件夹中,并修改app.json
为指向我的图标:
"expo": {
...
"icon": "./assets/myIcon.png"
}
Run Code Online (Sandbox Code Playgroud)
但启动时我从博览会收到此错误:
字段:图标 - 无法访问“./assets/myIcon.png”处的文件
我缺少什么?我应该把 Icon 图片放在哪里?
通过运行expo start -c
清除缓存解决。*注意:我花了几分钟才与我的展会客户端应用程序同步!