Reputation: 73
How can you compare two text objects in React Native.
Sorry if the question is too triv. but after an hour of probing all possibilities I can't find the correct one.
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{
...
}
Upvotes: 2
Views: 32676
Reputation: 470
For large Strings use this approach:
let questions="Hello how are you doing.........................are you fine?............................Whats your full name?.............";
let compareQuestions="Hello how are you doing.........................are you fine?............................Whats your full name?.............";
let comp= Array.from(questions).map((x) => {
return typeof x === 'string' ? x : '';
}).join('')==Array.from(compareQuestions).map((x) => {
return typeof x === 'string' ? x : '';
}).join('')
console.log("compare============",comp)
Upvotes: 0
Reputation: 53691
Ok, you could check the text and then set the state of a variable in the function that is called onChangeText. Here is an example, and I've pasted the code below as well.
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);
Upvotes: 4