英文:
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:
<div>
{data &&
Object.keys(data).map((keyId: string) => {
return <MyComponent keyId={keyId} key={keyId} />;
})
}
</div>
Then MyComponent
looks something like:
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} />
</>
);
};
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 && <myPopUpWindow/>}
Then just add to the handleRowClick function:
setShowWindow(true);
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论