2016-02-19 84 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);