简体   繁体   English

如何让传播运算符使用 React 更新 state

[英]How to get spread operator updating state with React

I've defined my initial state in a Component as follows:我在组件中定义了我的初始 state,如下所示:

constructor(props) {
    super(props);
    //this.state = {count: props.initialCount};
    this.state = {
        timeArray: [],
        metawords: '',
        description: '',
        currentTime: '',
        inputFieldsDisabled: true
    }
}

I have an event that gets called and I want to just update the metawords property.我有一个被调用的事件,我只想更新 metawords 属性。 I was thinking that the following code should work but it does not.我在想下面的代码应该可以工作,但事实并非如此。

updateInputValue1(evt) {
    const newMetawords = "abcd";
    this.setState(
        [...this.state,{
            metawords: evt.target.value
        }]
    );

Thoughts?想法?

state is an object so updating it the way you are at the moment won't work. state是一个对象,因此无法按当前state进行更新。

You can simply update only the property you need as: 您只需将所需的属性更新为:

this.setState({
  metawords: evt.target.value,
})

Since you've mentioned spread operator, you can also (but not always necessary) update your state as: 由于您已经提到了spread运算符,因此您也可以(但并非总是必要)将状态更新为:

this.setState({
  ...this.state,
  metawords: evt.target.value,
})

or 要么

this.setState(prevState => ({
  ...prevState,
  metawords: evt.target.value,
}))

Should you need more info about it, I recommend you to have a look at ReactJS documentation . 如果您需要有关它的更多信息,我建议您看一下ReactJS文档

You can use spread operator like this to setState. 您可以像这样使用spread运算符来设置状态。

this.setState(
        {...this.state,
          metawords: evt.target.value
        })

However since you only want to change a single property, this will also work in your case: 但是,由于您只想更改单个属性,因此这也适用于您的情况:

this.setState({metawords: evt.target.value})

Why not simply: 为什么不简单地:

this.setState({ metawords: evt.target.value })

You don't need to pass all other state values, just pass the property and the new value, that you want to update. 您不需要传递所有其他状态值,只需传递要更新的属性和新值。 You don't need to bother about the other state values during setState, React will do the merging (merging of all other state values and the state that you wants to update). 在setState期间,您无需理会其他状态值,React会进行合并(合并所有其他状态值和您要更新的状态)。

For more details check DOC . 有关更多详细信息,请检查DOC

What is wrong in this code.How to update that state....这段代码有什么问题。如何更新 state....

add = () => {
            const count = 0;
            this.state.videoLinks.forEach((item,index)=>{
                if(item.length===0){
                    count = 1;
                    // this.setState({...state, error: `Please fill the URL row${index+1}`});
                    this.setState(prevState => ({
                        ...prevState,
                        error: `Please fill the URL row${index+1}`
                      }))
                    return;
                }
            });
            if(count===0){
                // this.setState({ ...state, videoLinks: [...this.state.videoLinks].concat('') });
                this.setState(prevState => ({
                    ...prevState,
                    videoLinks: [...this.state.videoLinks].concat('')
                  }));
            }
            
        };

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM