如何从子组件更新 props.data?

发布于 2025-01-10 20:08:24 字数 753 浏览 0 评论 0原文

我正在尝试显示传递给子级的数组的范围。 我当前的父组件如下:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

dawn曙光 2025-01-17 20:08:25

无论您想在插入州之前检查的数字列表是什么,您都可以集体执行此操作

    const [updatedSymbols, setUpdatedSymbols] = useState()
    const range = (from, to ) => {
       var collection = [];
       for(let i = from; i<=to ; i++) {
         collection.push(i);
       }
    return collection;
   }
    useEffect(() => 
        if(range(1,100).includes(props.number) ){
                 setUpdatedSymbols(props.number)
                console.log("updated: " + props.number)
            }
        }, [props.symbol])
    
    return (
        <div class="cell">
            {updatedSymbols}
        </div>
    )

    // this is magical trick
    // [1,2,3,4,5].includes(props.number); //if props.number happens to be in the array then it i'll give true

whatever the numbers list you want to check before you insert into your state you can collectively do this

    const [updatedSymbols, setUpdatedSymbols] = useState()
    const range = (from, to ) => {
       var collection = [];
       for(let i = from; i<=to ; i++) {
         collection.push(i);
       }
    return collection;
   }
    useEffect(() => 
        if(range(1,100).includes(props.number) ){
                 setUpdatedSymbols(props.number)
                console.log("updated: " + props.number)
            }
        }, [props.symbol])
    
    return (
        <div class="cell">
            {updatedSymbols}
        </div>
    )

    // this is magical trick
    // [1,2,3,4,5].includes(props.number); //if props.number happens to be in the array then it i'll give true
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文