Javascript 导入提取请求?
我有一个react本机应用程序,它使用大量的获取请求。这些请求工作得很好,但我现在正试图将所有的fetch请求移动到一个文件中,并将它们导入到我的类中 问题是:我不知道如何正确地做到这一点。基本上,我可以从我的类中调用fetch方法,但基于成功或失败,我不知道如何使用它。见示例:Javascript 导入提取请求?,javascript,reactjs,react-native,Javascript,Reactjs,React Native,我有一个react本机应用程序,它使用大量的获取请求。这些请求工作得很好,但我现在正试图将所有的fetch请求移动到一个文件中,并将它们导入到我的类中 问题是:我不知道如何正确地做到这一点。基本上,我可以从我的类中调用fetch方法,但基于成功或失败,我不知道如何使用它。见示例: import stuff... import { FetchRequest } from ... class Home extends Component { constructor(props) {
import stuff...
import { FetchRequest } from ...
class Home extends Component {
constructor(props) {
...
}
_fetch() {
FetchRequest();
}
_onSuccess() {
alert('success!');
}
_onFailure() {
alert('failure!');
}
render() {
return <Button onPress={this._fetch} />
}
}
正如您所见,我可以轻松触发警报,还可以返回我不知道如何使用的字符串。基本上,如果FetchRequest成功,我想调用_onSuccess方法;如果FetchRequest失败,我想调用_onFailure方法。有什么方法可以实现这一点吗?
从函数返回fetch()
export const FetchRequest = function () {
return fetch(Url)
.then((response) => response.json())
.then((responseJson) => {
if (responseJson.status == 'success') {
return 'success';
}
else {
return 'failure';
}
})
.catch((error) => {
alert('Error!');
});
};
并将.then()
与\u onSuccess
和\u onFailure
作为参数传递
FetchRequest().then(this._onSuccess, this._onFailure);
从函数返回fetch()
export const FetchRequest = function () {
return fetch(Url)
.then((response) => response.json())
.then((responseJson) => {
if (responseJson.status == 'success') {
return 'success';
}
else {
return 'failure';
}
})
.catch((error) => {
alert('Error!');
});
};
并将.then()
与\u onSuccess
和\u onFailure
作为参数传递
FetchRequest().then(this._onSuccess, this._onFailure);
您可以通过向FetchRequest函数发送onSuccess和onFailure回调来实现这一点
按如下方式更改FetchRequest:
import { Url } from ...
export const FetchRequest = function (_onSuccess, _onFailure) {
fetch(Url)
.then((response) => response.json())
.then((responseJson) => {
if (responseJson.status == 'success') {
_onSuccess(responseJson);
}
else {
_onFailure(responseJson);
}
})
.catch((error) => {
alert('Error!');
});
};
您现在可以将其用作:
import stuff...
import { FetchRequest } from ...
class Home extends Component {
constructor(props) {
...
}
_fetch() {
FetchRequest(this._onSuccess, this._onFailure);
}
_onSuccess(responseJson) {
// Do something with the response
alert('success!');
}
_onFailure(responseJson) {
// Show the failed result
alert('failure!');
}
render() {
return <Button onPress={this._fetch} />
}
}
导入内容。。。
从…导入{FetchRequest}。。。
类Home扩展组件{
建造师(道具){
...
}
_fetch(){
FetchRequest(this.\u onSuccess,this.\u onFailure);
}
_onSuccess(responseJson){
//对回应做点什么
警惕(“成功!”);
}
_onFailure(responseJson){
//显示失败的结果
警报(“故障!”);
}
render(){
返回
}
}
您可以通过向FetchRequest函数发送onSuccess和onFailure回调来实现这一点
按如下方式更改FetchRequest:
import { Url } from ...
export const FetchRequest = function (_onSuccess, _onFailure) {
fetch(Url)
.then((response) => response.json())
.then((responseJson) => {
if (responseJson.status == 'success') {
_onSuccess(responseJson);
}
else {
_onFailure(responseJson);
}
})
.catch((error) => {
alert('Error!');
});
};
您现在可以将其用作:
import stuff...
import { FetchRequest } from ...
class Home extends Component {
constructor(props) {
...
}
_fetch() {
FetchRequest(this._onSuccess, this._onFailure);
}
_onSuccess(responseJson) {
// Do something with the response
alert('success!');
}
_onFailure(responseJson) {
// Show the failed result
alert('failure!');
}
render() {
return <Button onPress={this._fetch} />
}
}
导入内容。。。
从…导入{FetchRequest}。。。
类Home扩展组件{
建造师(道具){
...
}
_fetch(){
FetchRequest(this.\u onSuccess,this.\u onFailure);
}
_onSuccess(responseJson){
//对回应做点什么
警惕(“成功!”);
}
_onFailure(responseJson){
//显示失败的结果
警报(“故障!”);
}
render(){
返回
}
}
如何以这种方式将responseJson传递给成功或失败方法?从返回responseJson
。然后()
如何以这种方式将responseJson传递给成功或失败方法?从返回responseJson
。然后()