如何在reactJS的嵌套状态对象中使用reduce函数? [英] How to use reduce function in a nested state object in reactJS?
本文介绍了如何在reactJS的嵌套状态对象中使用reduce函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我的状态值为
this.state = {
content: {
text: {
tag1: {
line: "data1"
}
tag2: {
line: "data2"
}
}
}
}
如何使用javascript reduce()
函数将tag1
和tag2
的line
值都更改为"changed text"
?
How can I use javascript reduce()
function to change the value of line
of both 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"
}
}
}, {})
}
}
});
这篇关于如何在reactJS的嵌套状态对象中使用reduce函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文