亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

React Native 設置函數調用流程

React Native 設置函數調用流程

藍山帝景 2023-07-06 17:37:57
我寫了這段代碼componentDidMount(){        Geolocation.getCurrentPosition(info => {        console.log(info.coords.latitude + "    " + info.coords.longitude)        this.setState({coords: {latitude: info.coords.latitude, longitude: info.coords.longitude, latitudeDelta: this.LATITUDE_DELTA, longitudeDelta: this.LONGITUDE_DELTA}})    }, error => Alert.alert('Error', JSON.stringify(error)),    {enableHighAccuracy: true, timeout: 20000, maximumAge: 1000});          console.log(this.state.coords.latitude + "    " + this.state.coords.longitude);    region = getapi(this.state.coords.latitude, this.state.coords.longitude)    this.setState({region})    console.log("TEMP: " + region);    hot = getHotBarb(this.state.region)    this.setState({hot})    rated = getRatedBarb(this.state.region)    this.setState({rated})    offer = getOfferBarb(this.state.region)    this.setState({offer})    this.setState({loading: false})}但在日志控制臺上,我首先看到 this.state.coords.latitude + " " + this.state.coords.longitude (未定義)的日志和 getCurrentPosition 調用內的日志(正確的)。問題是我無法調用函數 getapi 因為 this.state.coords.latitude, this.state.coords.longitude 結果未定義 我需要先設置this.state.coords.latitude然后this.state.coords.longitude調用 中的其他函數componentDidMount()。如何在其他函數之前調用 getCurrentPosition 函數上的 setState?
查看完整描述

1 回答

?
慕的地10843

TA貢獻1785條經驗 獲得超8個贊

試試這個方法


callApi(){


    console.log(this.state.coords.latitude + "    " + this.state.coords.longitude);


    region = getapi(this.state.coords.latitude, this.state.coords.longitude)

    this.setState({region})

    console.log("TEMP: " + region);

    hot = getHotBarb(this.state.region)

    this.setState({hot})

    rated = getRatedBarb(this.state.region)

    this.setState({rated})

    offer = getOfferBarb(this.state.region)

    this.setState({offer})


    this.setState({loading: false})

}



getCurrentPosition(){


    Geolocation.getCurrentPosition(info => {

        console.log(info.coords.latitude + "    " + info.coords.longitude);


        this.setState({coords: {latitude: info.coords.latitude, longitude: info.coords.longitude, latitudeDelta: this.LATITUDE_DELTA, longitudeDelta: this.LONGITUDE_DELTA}},

    () => this.callApi();  // call api here

    )


    }, error => Alert.alert('Error', JSON.stringify(error)),

    {enableHighAccuracy: true, timeout: 20000, maximumAge: 1000});

}



componentDidMount(){    

    this.getCurrentPosition();

}


查看完整回答
反對 回復 2023-07-06
  • 1 回答
  • 0 關注
  • 85 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號