从视图反应本地API调用

时间:2018-08-22 16:02:21

标签: reactjs react-native

我有一个我正在做的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调用,但似乎无法实现。我该怎么办?

1 个答案:

答案 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>
       )
   }