2016-02-19 6 views
0

Как вы можете сравнить два текстовых объекта в React Native.Сравнить текст в native-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); 
Смежные вопросы