gpt4 book ai didi

javascript - onChangeText + setState 不通过深度合并更新对象值

转载 作者:行者123 更新时间:2023-11-28 03:03:27 25 4
gpt4 key购买 nike

我试图通过设置组件的状态来更新组件的状态,但即使在触发 onChangeText 后,我​​的文本也没有更新。

export default class Register extends React.Component {

constructor(props) {
super(props)
this.state = {

newUser: {
id: "",
first_name: "Tom",
last_name: "",
email: "",
age: 0,
gender: "",
classification: "",
major: "",
interest_tags: new Set()
}

}
}

updateUser = (text) =>{

}
render() {

return (

<View style={styles.container}>
<View style={styles.welcome}>
<Text style={styles.welcomeText}>Enter your information below:</Text>
</View>
<View style={styles.inputView}>
<TextInput
style={styles.inputText}
placeholder='First Name'
placeholderTextColor='#34415e'
autoCapitalize='none'
onChangeText={(text) => this.setState({
...this.state.newUser,
first_name: text
}), () => {console.log(this.state.newUser.first_name)}}

/>

</View>

无论我将回调放在 updateUser 函数中还是渲染函数中,我都无法更新 Tom 的first_name 值。我在这里做错了什么?

最佳答案

你能试试这个吗

onChangeText={(text) => this.setState({
newUser : {
...this.state.newUser,
first_name: text,
}
}), () => {console.log(this.state.newUser.first_name)}

关于javascript - onChangeText + setState 不通过深度合并更新对象值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60795942/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com