第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會有你想問的

onChangeText + setState 不使用深度合并更新對象值

onChangeText + setState 不使用深度合并更新對象值

浮云間 2022-05-26 14:51:20
我試圖通過設(shè)置它的狀態(tài)來更新我的組件的狀態(tài),但即使在 onChangeText 被觸發(fā)后,我的文本也沒有更新。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>無論我將回調(diào)放在 updateUser 函數(shù)中還是在渲染函數(shù)中,都無法更新 Tom 的 first_name 值。我在這里做錯(cuò)了什么?
查看完整描述

3 回答

?
哆啦的時(shí)光機(jī)

TA貢獻(xiàn)1779條經(jīng)驗(yàn) 獲得超6個(gè)贊

“使用檢查員,盧克!” ~ 歐比旺 Reactnobi


在您的setState通話中,您正在合并狀態(tài)本身的內(nèi)容newUser以及first_name狀態(tài)本身,而不是狀態(tài)的newUser字段。因此,在更新狀態(tài)后,您最終會得到以下數(shù)據(jù):


{

  newUser: { /* the initial value*/ },


  // all the other proprerties inside `newUser`, such as

  email: "",

  age: 0,


  // then the key you merged into the state explicitly:

  first_name: "whatever the user typed"

}

你可能打算這樣寫(記住這setState已經(jīng)是一個(gè)淺合并,所以只有newUser在你的狀態(tài)對象中有更多值時(shí)才會更新):


this.setState({

  newUser: {

    ...this.state.newUser,

    first_name: text

  }

})


查看完整回答
反對 回復(fù) 2022-05-26
?
搖曳的薔薇

TA貢獻(xiàn)1793條經(jīng)驗(yàn) 獲得超6個(gè)贊

你能試試這個(gè)


onChangeText={(text) => this.setState({

      newUser : {

         ...this.state.newUser,

         first_name: text,

      }

}), () => {console.log(this.state.newUser.first_name)}


查看完整回答
反對 回復(fù) 2022-05-26
?
森欄

TA貢獻(xiàn)1810條經(jīng)驗(yàn) 獲得超5個(gè)贊

首先,您必須僅使用根對象更新狀態(tài)


你不能像那樣更新狀態(tài)


onChangeText={(text) => this.setState({

                            ...this.state.newUser,

                            first_name: text

                            }), () => {console.log(this.state.newUser.first_name)}}

讓我給你我的自定義函數(shù)。


updateObject = (oldObject, updatedProperties) => {

return {

    ...oldObject,

    ...updatedProperties

};};

你可以這樣使用


 onChangeText={(text) => {

       let updatedFormElement ;

        updatedFormElement = this.updateObject(this.state.newUser, {

            first_name: text

        });

        this.setState({ newUser: updatedFormElement });

    }}   


查看完整回答
反對 回復(fù) 2022-05-26
  • 3 回答
  • 0 關(guān)注
  • 130 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號