Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

React Native console log proxy object

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.

Here is the simulator. enter image description here

I was expecting that it will print 1, 12 and 123 in chrome console.

But I got this enter image description here

Why it prints this proxy object, and what is this object?

like image 745
Coda Chang Avatar asked Mar 27 '26 20:03

Coda Chang


1 Answers

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)
})
like image 188
jmac Avatar answered Mar 29 '26 10:03

jmac



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!