Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
131 views
in Technique[技术] by (71.8m points)

react setState

我的state

this.state = {
    text: '',
    form: {
        aaa: '',
        bbb: ''
    }
}

我查阅了资料,当key为变量时可以这样写:

this.setState({
    [key]:value
})    

但是,我这个key是在form对象里的时候该咋写呢,我下面这样写直接报错了

this.setState({
    form[[key]]:value
}) 

救救孩子吧!!!呜呜呜


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
this.setState({
    form: {
        ...this.state.form,
        [key]: value
    }
})

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...