RECT-使用使用效率回调的状态
我以我的问题为例。我正在构建一个简单的组件,该组件应该管理孩子的大小,但是在进行任何操纵之前,需要了解孩子的最初大小。
因此,每个孩子都会在使用
上向父组件报告自己的大小,而父组件必须保留所有孩子的宽度,但只保留最后一个。
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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
当您调用
setWidths
时,您想传递一个获得最新状态值的回调,例如: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:Demo: https://stackblitz.com/edit/react-ts-kzgjfv?file=index.tsx