如何在reactJS中的嵌套状态对象中使用reduce函数?

2024-02-20

我的状态值为

this.state = {
    content: {
        text: {
            tag1: {
                line: "data1"
            }
            tag2: {
                line: "data2"
            }
        }
    }
}

我怎样才能使用JavaScriptreduce()函数来改变值line两者的tag1 and tag2 to "changed text"?


给你:

this.setState(prevState => {
    return {
        content: {
            ...prevState.content,
            text: Object.keys(prevState.content.text).reduce((newTexts, key) => {
                return {
                    ...newTexts,
                    [key]: {
                        line: "changed text"
                    }
                }
            }, {})
        }
    }
});
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何在reactJS中的嵌套状态对象中使用reduce函数? 的相关文章

随机推荐