2017-06-20 5 views
0

私は、メインコンポーネントのcomponentWillMountにコードを追加するとすぐにレンダリングするアラートコンポーネントを持っています。 The project is an expo project可視性状態が変更されていない場合、反応ネイティブの警告コンポーネントが2回表示されるのはなぜですか?

アラートコンポーネントの可視性を制御するプロパティをfalseに設定し、次にcomponentWillMountプロシージャ内の可視性状態を変更するthis.startAlert()を呼び出すことで回避しました。回避策を適用する前に、ダイアログのボタンを押した後にだけ表示状態が変更されました。

これは機能します。アラートダイアログを1回表示します。 this.startAlert();約束で実行されます。

componentWillMount() { 

    const setPosition = (pos) => { 

     console.log(pos) ; 

    // We need the "latitude" and the "longitude" 
    // in order to lookup the "address" from the 
    // Google maps API. 
    const { 
     coords: { 
     latitude, 
     longitude, 
     }, 
    } = pos; 

    // Fetches data from the Google Maps API then sets 
    // the "address" state based on the response. 
    fetch(`${URL}${latitude},${longitude}`) 
     .then(resp => resp.json(), e => console.error(e)) 
     .then(({ 
     results: [ 
      { formatted_address }, 
     ], 
     }) => { 
      this.setAddress(formatted_address) ; 
      this.startAlert() ;   
     }); 
    } ; 


    navigator.geolocation.getCurrentPosition(setPosition) ; 

} 

これは失敗します。警告コンポーネントが2回表示されます。 this.startAlert();フェッチの約束の外に呼び出されます。

componentWillMount() { 

    const setPosition = (pos) => { 

     console.log(pos) ; 

    // We need the "latitude" and the "longitude" 
    // in order to lookup the "address" from the 
    // Google maps API. 
    const { 
     coords: { 
     latitude, 
     longitude, 
     }, 
    } = pos; 

    // Fetches data from the Google Maps API then sets 
    // the "address" state based on the response. 
    fetch(`${URL}${latitude},${longitude}`) 
     .then(resp => resp.json(), e => console.error(e)) 
     .then(({ 
     results: [ 
      { formatted_address }, 
     ], 
     }) => { 
      this.setAddress(formatted_address) ;  
     }); 
    } ; 

    this.startAlert() ; 
    navigator.geolocation.getCurrentPosition(setPosition) ; 

} 

可視性状態が変更されていない場合、反応ネイティブの警告コンポーネントが2回表示されるのはなぜですか?

編集:アドレスを設定するコード。

// Getter for "Immutable.js" state data... 
get data() { 
    return this.state.data; 
} 

// Setter for "Immutable.js" state data... 
set data(data) { 
    this.setState({ data }); 
} 

setAddress = (address) => { 
    this.data = this.data.set('address', address) ; 
} 
+0

'this.setAddress'は正確に何ですか?状態を更新しますか? – Raymond

+0

なぜ2つのsetPosition関数がありますか? –

+0

this.setAddressは状態を更新します。アドレスを設定します。 –

答えて

1

は一見、彼らはほとんど同じに見える読書の少しでもで以来、2つのコードスニペットの間の特定の違いを強調すると便利でした。 this.setAddress(...)の定義は含まれていないようですが、それは一時的にthis.setState(...)と呼びますか? this.setState(...)は、thisの再レンダリングを引き起こします。

関連する問題