2016-12-14 2 views
4

私はいくつかのイベントに動的に値を変更したい 値を変更する はネイティブリアクト:どのように動的に

イベント:

BackgroundGeolocation.on('location', (location) => {  
    currentDistance = distance(previousLatitude,previousLongitude,latitude,longitude); 

      this.setState({ 
       text: currentDistance     
      });         

    }); 
<Text> 
    Moving : {this.state.text} 
</Text> 

は、誰もがテキストまたは達成するための他の方法を変更する方法を知っているのですか?

+0

を変更したいです正確に何を?どのようなイベントですか? – Tikkes

+0

'this.setState({text: '別のテキスト'})' http://facebook.github.io/react-native/releases/0.38/docs/state.html – Cherniv

+0

私の場所で計算する距離を更新したいイベント 質問が更新されました 未定義は関数ではありません(this.setState({text:currentDistance})を評価しています) –

答えて

14

以下は、テキスト値をクリックしながら動的に変更するために状態を使用する例です。任意のイベントに設定できます。

import React, { Component } from 'react' 
import { 
    Text, 
    View 
} from 'react-native' 

export default class reactApp extends Component { 
    constructor() { 
     super() 
     this.state = { 
     myText: 'My Original Text' 
     } 
    } 
    updateText =() => { 
     this.setState({myText: 'My Changed Text'}) 
    } 
    render() { 
     return (
     <View> 
      <Text onPress = {this.updateText}> 
       {this.state.myText} 
      </Text> 
     </View> 
    ); 
    } 
} 
+0

@ 感謝して達成している修正https://rnplay.org/apps/J417aA –

+0

私はちょうど希望の引数でupdateText()を呼び出す必要があります その仕事の男性 それを愛する!!!! ありがとうございます... –

2

用途:

<TextInput editable={false} ref={component=> this._MyComponent=component}/> 

の代わり:

<Text></Text> 

次にあなたがテキストをこのように変更することができます。

onPress= {()=> { 
    this._MyComponent.setNativeProps({text:'my new text'}); 
} 
関連する問題