嵌套 React 组件重新渲染和 React 协调
我正在努力理解处理嵌套反应组件时协调的工作原理。下面是我的index.js 代码
import React from "react";
import { render } from "react-dom";
import { useState, useEffect } from "react";
const TodoList = ({ todos }) => {
useEffect(() => {
console.log("mounted List")
return () => {
console.log("unmounted List")
}
}, [])
const Todo = ({ txt }) => {
useEffect(() => {
console.log("mounted Todo")
return () => {
console.log("unmounted Todo")
}
}, [])
return (
<li>
{txt}
</li>
);
};
return todos.map(todo => <Todo {...todo} />);
};
const App = () => {
const [todos, setIt] = useState([{ txt: "foo" }, { txt: "bar" }, { txt: "baz" }])
return (
<>
<button onClick={() => setIt(todos.concat({ txt: 'new' }))}>Click</button>
<TodoList todos={todos} />
</>)
}
render(<App />, document.getElementById("root"));
下面是index.html
<div id="root"></div>
令我困惑的部分是为什么每次我单击按钮时所有反应实例都会继续卸载和重新安装。我的理解是,react 实例的“类型”并不相同,因为我们在 TodoList 组件内部定义了 Todo 组件本地范围。因此,我们基本上每次在重新渲染期间都会创建一个新的 Todo 组件。然而,我觉得我的解释是一个非常高级的答案(另外,我什至不确定我是否正确),并且我不确定关于反应协调器如何管理之间的区别的较低级别的实现细节当我们有嵌套组件和非嵌套组件时,反应实例的“类型”。 (我使用术语“嵌套”和“非嵌套”来表示在另一个反应组件内部定义一个反应组件,就像我给出的示例一样)。
I'm struggling with understanding how reconciliation works when dealing with nested react components. Below is my code for index.js
import React from "react";
import { render } from "react-dom";
import { useState, useEffect } from "react";
const TodoList = ({ todos }) => {
useEffect(() => {
console.log("mounted List")
return () => {
console.log("unmounted List")
}
}, [])
const Todo = ({ txt }) => {
useEffect(() => {
console.log("mounted Todo")
return () => {
console.log("unmounted Todo")
}
}, [])
return (
<li>
{txt}
</li>
);
};
return todos.map(todo => <Todo {...todo} />);
};
const App = () => {
const [todos, setIt] = useState([{ txt: "foo" }, { txt: "bar" }, { txt: "baz" }])
return (
<>
<button onClick={() => setIt(todos.concat({ txt: 'new' }))}>Click</button>
<TodoList todos={todos} />
</>)
}
render(<App />, document.getElementById("root"));
Below is index.html
<div id="root"></div>
The part that's confusing me is why all of the react instances keep on un-mounting and re-mounting every time I click the button. My understanding is that the "type" of the react instances aren't the same because we define the Todo component locally scoped inside of the TodoList component. Due to this, we are basically creating a new Todo component every-time during re-render. However, I feel that my explanation is a very high-level answer (Also, I'm not even sure if I'm correct) and I'm unsure of the lower-level implementation details on how the react reconciler manages distinctions between the "type" of react instances when we have nested vs un-nested components. (I'm using the term "nested" and "un-nested" as meaning defining a react component inside of the another react component like in the example I gave).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
Todo 组件已卸载,因为状态更改时 TodoList 会重新呈现。
如果将 Todo 组件保留在 TodoList 组件之外,一旦状态发生变化,它就不会卸载。
Todo component is unmounted because TodoList rerenders when state is changed.
If you keep you Todo component outside of the TodoList component it will not unmount once state changes.