为每个弹出窗口创建一个实例,而不是渲染所有内容。

huangapple go评论61阅读模式
英文:

Create instance for each popup window instead of rendering everything

问题

I'll provide a translation of the code portion you shared:

我有一个设置在此设置中我获取用于页面的某些数据首先我以以下方式迭代其中的一些数据

    <div>
        {data &&
             Object.keys(data).map((keyId: string) => {
                 return <MyComponent keyId={keyId} key={keyId} />;
             })
        }
    </div>

然后`MyComponent` 大致如下

    const MyComponent= ({ keyId }: Props) => {
        const myData = myZustandStore((state) => state.myData);
    
        const [data, setData] = useState({});
    
        useEffect(() => {
            setData(myData[keyId]);
        }, [myData, keyId]);
    
        const [isWindow, setIsWindow] = useState(false);
    
        const handleRowClick = () => {
            setIsWindow(true);
        };
    
        const handleWindowClose = () => {
            setIsWindow(false);
        };
    
        return (
            <>
                <div onClick={handleRowClick}>
                    Click
                </div>
    
                <MyPopupWindow windowOpen={isWindow} handleWindow={handleWindowClose} data={data} keyId={keyId} />
            </>
        );
    };

Please note that this is a translation of the code portion only, as per your request.

英文:

I have have a setup where I fetch some data that is used on a page. To start off I iterate over some of the data like this:

&lt;div&gt;
    {data &amp;&amp;
         Object.keys(data).map((keyId: string) =&gt; {
             return &lt;MyComponent keyId={keyId} key={keyId} /&gt;;
         })
    }
&lt;/div&gt;

Then MyComponent looks something like:

const MyComponent= ({ keyId }: Props) =&gt; {
    const myData = myZustandStore((state) =&gt; state.myData);

    const [data, setData] = useState({});

    useEffect(() =&gt; {
        setData(myData[keyId]);
    }, [myData, keyId]);

    const [isWindow, setIsWindow] = useState(false);

    const handleRowClick = () =&gt; {
        setIsWindow(true);
    };

    const handleWindowClose = () =&gt; {
        setIsWindow(false);
    };

    return (
        &lt;&gt;
            &lt;div onClick={handleRowClick}&gt;
                Click
            &lt;/div&gt;

            &lt;myPopupWindow windowOpen={isWindow} handleWindow={handleWindowClose} data={data} keyId={keyId} /&gt;
        &lt;/&gt;
    );
};

So let's say I have 10 items in my data which I iterate over, giving me 10 divs I can click. As such this work, however, what I am effectively doing is renderen a myPopupWindow for every item, and then with handleRowClick I am just showing the pop-up window that needs to be shown. Hence, whatever I do inside myPopupWindow, e.g. calculations, renderings, etc. is actually done, I just can't see the result unless I click the the item.

In my opinion that just seems like a waste, i.e. rendering everything. In particular if I at some point get 1000 items or more. So what I am asking is: There has to be a more appropriate way to do these things with popups, where it only renders IF it is called?

I've tried putting something like:

if (!WindowOpen) {
    return null;
}

At the top of myComponent. And that somewhat works, but again, it seems like that is not the proper way to do it. Also, it actually fails during builds due to some hook ordering, where it wants it at the bottom of the component, where all rendering has been done anyways.

答案1

得分: 2

你可以创建一个状态变量来控制是否渲染弹出窗口,像这样:

const [showWindow, setShowWindow] = useState(false);

然后将其用作渲染的条件:

{showWindow && <myPopUpWindow/>}

然后只需在handleRowClick函数中添加以下代码:

setShowWindow(true);
英文:

You can create a state variable to control if the popup window will be rendered at all like so:

const [showWindow, setShowWindow] = useState(false);

Then use it as a conditional for rendering:

{showWindow &amp;&amp; &lt;myPopUpWindow/&gt;}

Then just add to the handleRowClick function:

setShowWindow(true);

huangapple
  • 本文由 发表于 2023年7月6日 21:25:25
  • 转载请务必保留本文链接:https://go.coder-hub.com/76629342.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定