RECT-使用使用效率回调的状态

发布于 2025-01-23 16:10:13 字数 400 浏览 1 评论 0原文

我以我的问题为例。我正在构建一个简单的组件,该组件应该管理孩子的大小,但是在进行任何操纵之前,需要了解孩子的最初大小。

因此,每个孩子都会在使用上向父组件报告自己的大小,而父组件必须保留所有孩子的宽度,但只保留最后一个。

https://stackblitz.com/edit/edit/reactt-ts-ts-fdqv7x? file = index.tsx

问题是为什么widths的值是空的:26?

I've made an example of my question. I'm building a simple component that should manage the size of its children, but before any manipulation, needs to know the children's initial size.

So every child reports its own size on useEffect to the parent component, and parent component must keep the widths of all children, but only keeps the last one.

https://stackblitz.com/edit/react-ts-fdqv7x?file=index.tsx

the question is Why the value of widths is empty on line no: 26?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

_畞蕅 2025-01-30 16:10:13

当您调用setWidths时,您想传递一个获得最新状态值的回调,例如:

setWidths((w) => ({ ...w, [index]: width }));

demo: https://stackblitz.com/edit/react-ts-kzgjfv?file=index.tsx

When you're calling setWidths you want to pass a callback that gets the latest state value, like this:

setWidths((w) => ({ ...w, [index]: width }));

Demo: https://stackblitz.com/edit/react-ts-kzgjfv?file=index.tsx

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文