简体   繁体   English

React 父组件是否等待子组件渲染?

[英]Does react parent component wait for the children to render?

I have a parent component that has 800 children (a list).我有一个父组件,它有 800 个子组件(一个列表)。

I want to scroll the list to a pre-defined position X.我想将列表滚动到预定义的位置 X。

I have something like this in the Parent:我在父级中有这样的东西:

useEffect(() => /* scroll the list */, [])

Does the parent component's useEffect hook get called only after all children have been rendered or not?父组件的useEffect钩子是否仅在所有子组件都已渲染后才被调用?

I'm having a bug when I'm trying to scroll toward the end of the list.当我试图滚动到列表末尾时出现错误。 It feels like the end of the list doesn't exist yet when useEffect is called.调用useEffect时,感觉列表的末尾还不存在。

const C1: React.FC = ({ children }) => {
console.log(1);

React.use Effect(() => {
console.log(2)});

return <>{children}</>
}

const C2: React.FC = () => {
console.log(3);

React.use Effect(() => {
console.log(4)});

return <div>YOWZA</div>
}

const App = () => <C1><C2/></C1>
render(<App />, document.getElementById('root'));

Order of console.log are 1,3,4,2. console.log 的顺序是 1,3,4,2。

I hope this helps我希望这有帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM