Reputation: 4634
I was following a react-native
tutorial, but I encounter some weird result.
Here is the code
module.exports = React.createClass({
getInitialState(){
return({
tasks: ['Take Course', "Clean house"],
task: ''
})
},
render() {
return(
<View style={styles.container}>
<Text style={styles.header}>
ToDoList
</Text>
<TextInput
style = {styles.inputbox}
placeholder="Type task"
onChange={(text) => {
this.setState({task: text});
console.log(this.state.task);
}}
/>
</View>
)
}
})
I want to use console.log
to print text
which is coming from TextInput
.
I was expecting that it will print 1
, 12
and 123
in chrome console.
Why it prints this proxy
object, and what is this object?
Upvotes: 3
Views: 2213
Reputation: 706
Use onChangeText
instead of onChange
. Take a look at https://facebook.github.io/react-native/docs/textinput.html
Moreover this.setState
is an asynchronous call, that means it is undetermined when state will be set. In that case you have to call console.log as a callback to be sure that state has changed.
this.setState({
task: text
}, () => {
console.log(this.state.task)
})
Upvotes: 4