如何从子组件更新 props.data?
我正在尝试显示传递给子级的数组的范围。 我当前的父组件如下:
import data from './data.json'
return (
<Cell symbol={data.symbol} number={data.number} />
)
并且该数据正在传递到我的子组件中:
const [updatedSymbols, setUpdatedSymbols] = useState()
useEffect(() =>
if(props.number === 1 || props.number === 2 || props.number === 3 ){
setUpdatedSymbols(props.number)
console.log("updated: " + props.number)
}
}, [props.symbol])
return (
<div class="cell">
{updatedSymbols}
</div>
)
问题:如果您查看 useEffect,您会注意到在 if 语句中,我正在选择数字我想要并将它们简单地传递到 useState“setUpdatedSymbols”中。 我的问题是,我需要选择的数字太多,大约 100 个,如何在不使用 || 的情况下将它们全部推入 updateSymbols 中?
I am trying to display a range from an array that is being passed into a child.
My current Parent component is as follows:
import data from './data.json'
return (
<Cell symbol={data.symbol} number={data.number} />
)
And that data is being passed into my Child component:
const [updatedSymbols, setUpdatedSymbols] = useState()
useEffect(() =>
if(props.number === 1 || props.number === 2 || props.number === 3 ){
setUpdatedSymbols(props.number)
console.log("updated: " + props.number)
}
}, [props.symbol])
return (
<div class="cell">
{updatedSymbols}
</div>
)
QUESTION: If you look at the useEffect, you will notice that within the if-statement, I am selecting the numbers I want and simply passing them into the useState, "setUpdatedSymbols".
My problem is that there are too many numbers that I need to select, about 100, how can I push them all into updatedSymbols without using || ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
无论您想在插入州之前检查的数字列表是什么,您都可以集体执行此操作
whatever the numbers list you want to check before you insert into your state you can collectively do this