React useEffect执行顺序
我有一个代码段,它使用了两个组件 - c1
和c2
:
import "./styles.css";
import React from 'react';
const C1 = ({ children }) => {
console.log('1');
React.useEffect(() => {
console.log('2');
}, []);
return <>{children}</>;
};
const C2 = () => {
console.log('3');//
React.useEffect(() => {
console.log('4');
}, []);
return <div>YOWZA</div>;
};
export default function App() {
return (<C1>
<C2/>
</C1>)
}
我正在尝试了解组件呈现的顺序。控制台显示这样的顺序:
1 1 3 3 4 2 4 2
我不明白为什么显示了两次1和3。另外,如果4和2在组件更新上不重新渲染,为什么显示4和2?
I have a snippet of code that uses two components - C1
and C2
:
import "./styles.css";
import React from 'react';
const C1 = ({ children }) => {
console.log('1');
React.useEffect(() => {
console.log('2');
}, []);
return <>{children}</>;
};
const C2 = () => {
console.log('3');//
React.useEffect(() => {
console.log('4');
}, []);
return <div>YOWZA</div>;
};
export default function App() {
return (<C1>
<C2/>
</C1>)
}
I am trying to understand the order of components render. The console shows order like this:
1 1 3 3 4 2 4 2
I do not understand why 1 and 3 were shown twice. Also, why 4 and 2 are shown twice if they do not re-render on component update?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论