我正在学习 react-native
教程,但我遇到了一些奇怪的结果。
这是代码
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>
)
}
})
我想使用 console.log
打印来自 TextInput
的 text
。
我原以为它会在 chrome 控制台中打印 1
、12
和 123
。
为什么打印这个proxy
对象,这个对象是什么?
最佳答案
使用 onChangeText
而不是 onChange
。看看https://facebook.github.io/react-native/docs/textinput.html
此外,this.setState
是一个异步调用,这意味着它何时设置状态是不确定的。在这种情况下,您必须调用 console.log 作为回调以确保状态已更改。
this.setState({
task: text
}, () => {
console.log(this.state.task)
})
关于javascript - React Native 控制台日志代理对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41210687/