2016-02-19 63 views
0

如何比較React Native中的兩個文本對象。比較反應本機中的文本

對不起,如果這個問題太過分了。但經過一小時探索所有可能性,我找不到正確的一個。

constructor(props) { 
super(props) 
this.state = { text: ''} 
} 

render() { 
return (
    <View style={styles.container}> 
    <TextInput 
     style={styles.inputText} 
     onChangeText={this.onChangeDo(this)} 
    /> 
    </View> 
); 
} 

    onChangeDo(event){ 
    (text) => this.setState({text}) 
    if(text equals 'string'){ 
    ... 
} 
else{ 
... 
} 
+1

你可以顯示兩個項目你可能會比較的,你在做什麼樣的比較的例子嗎?謝謝。 –

+0

我編輯了這個問題。 – user5918250

回答

0

好的,你可以檢查文本,然後在函數中調用onChangeText設置變量的狀態。 Here就是一個例子,我也粘貼了下面的代碼。

https://rnplay.org/apps/pWvSsg

'use strict'; 

var React = require('react-native'); 
var { 
    AppRegistry, 
    StyleSheet, 
    Text, 
    View, 
    TextInput, 
    Component 
} = React; 

class SampleApp extends Component { 

    constructor(props) { 
    super(props) 
    this.state = { text: ''} 
    } 

    onChangeDo(text) { 
    this.setState({ text }) 
    if(text == 'Hello') { 
     return this.setState({ hello: true }) 
    } 
    this.setState({ hello: false }) 
    } 

    render() { 
    return (
     <View style={styles.container}> 
     <TextInput 
      placeholder="Type Hello" 
      style={styles.inputText} 
      onChangeText={ (text) => this.onChangeDo(text) } 
      /> 
     { this.state.hello && <Text style={ styles.hello }>Hello World</Text> } 
     </View> 
    ); 
    } 
} 

var styles = StyleSheet.create({ 
    container: { 
    flex: 1, 
    marginTop: 60 
    }, 
    inputText: { 
    height:60, 
    backgroundColor: '#ededed' 
    }, 
    hello: { 
    fontSize:22 
    } 
}) 

AppRegistry.registerComponent('SampleApp',() => SampleApp);