我有一个我正在做的React Native应用-
我的APICall.js
中有-
export const myAPICall = async () => {
const reqOptions = {
method: 'GET',
};
let response = fetch('https://...', reqOptions);
return response;
}
我的View.js
在哪里-
import {myAPICall} from './APICall';
gotoView2 = (apiMethod) => {
this.props.navigation.navigate('View2', { apiMethod: apiMethod });
}
render() {
return(
<TouchableOpacity
onPress={() => this.gotoView2 (myAPICall)}>
<Text>Welcome</Text>
</TouchableOpacity>
)
}
其中apiMethod是myAPICall
在我的View2.js
中,我有-
doCall = async() => {
let response = await this.props.navigation.state.params.apiMethod();
}
但是什么也没发生。我想从View2.js进行API调用,但似乎无法实现。我该怎么办?
答案 0 :(得分:0)
尝试一下。
export const myAPICall = async () => {
const reqOptions = {
method: 'GET',
};
let response = await fetch('https://...', reqOptions);
return response;
}
import {myAPICall} from './APICall';
gotoView2 = () => {
myAPICall().then((returnData) => {
this.props.navigation.navigate('View2', { data: returnData });
});
}
render() {
return(
<TouchableOpacity
onPress={() => this.gotoView2()}>
<Text>Welcome</Text>
</TouchableOpacity>
)
}