英文:
useEffect renders the other components even if I pass the empty array
问题
已经在标题中指定,即使我传递空数组,useEffect
仍然会渲染其他组件。
我是一个刚开始学习 React 的女孩;我无法弄清楚为什么如果我传递空数组或 name
到依赖数组中,行为仍然相同,但实际上不应该。
这是我的代码:
App.js
export default function App() {
return (
<>
{/* 第一次渲染 */}
<Greet name="Eric" />
{/* 第二次渲染,name prop 改变 */}
<Greet name="Stan" />
{/* 第三次渲染,name prop 改变 */}
<Greet name="Butters" />
</>
)
}
Greet.js
import { useEffect } from "react";
export default function Greet({ name }) {
const message = `Hello, ${name}!`;
useEffect(() => {
console.log(`向 ${name} 打招呼`); // 副作用!
}, []);
return (
<div>{message}</div>
)
}
从我的理解来看,使用空数组时,它只需要渲染第一个组件。
英文:
As already specified in the title, useEffect renders the other components even if I pass the empty array.
I'm a girl who is just starting to learn react; i can't figure out why if i pass empty array or 'name' in dependency array the action is still the same, but it shouldn't be.
This is my code:
App.js
export default function App() {
return (
<>
{/* First render */}
<Greet name="Eric" />
{/* Second render, name prop changes */}
<Greet name="Stan" />
{/* Third render, name prop changes */}
<Greet name="Butters" />
</>
)
}
Greet.js
import { useEffect } from "react";
export default function Greet({ name }) {
const message = `Hello, ${name}!`;
useEffect(() => {
console.log(`Greetings to ${name}`); // Side-effect!
}, []);
return (
<div>{message}</div>
)
}
From what I understand, with the empty array it only needs to render the first component
答案1
得分: 3
你需要理解这里的三个概念。
首先是可重用组件。当你在 App
中写类似这样的代码时:
<Greet name="Eric" />
<Greet name="Stan" />
<Greet name="Butters" />
它在 HTML 中会类似于这样:
<div>Eric</div>
<div>Stan</div>
<div>Butters</div>
因此,在 App 组件中,你调用了 Greet 组件 3 次。它创建了 3 个 Greet 组件的实例。
其次是useEffect,当你提供一个空的依赖数组 []
作为 useEffect
的第二个参数时,意味着该效果只会在初始渲染后运行一次。它不会在组件重新渲染时再次运行。
在你的情况下,App 组件内有 3 个 Greet 组件的实例,它在 3 个实例中运行了 1 次,所以你看到了 3 次 console.log 显示。
第三是当某些内容发生变化时,React 如何重新渲染。基本上,React 需要某种方式通知它数据已经改变,需要相应地重新渲染组件。一个简单的方法是使用useState来处理数据。
因此,示例将如下所示:
import React, { useState } from "react";
function App() {
const [name, setName] = useState("Eric");
return (
<>
<Greet name={name} />
<button onClick={() => setName('Stan')}>Change to Stan</button>
<button onClick={() => setName('Butters')}>Change to Butters</button>
</>
);
}
function Greet({ name }) {
const message = `Hello, ${name}!`;
useEffect(() => {
console.log(`1st name ${name}`); // 副作用!
}, []);
useEffect(() => {
console.log(`name changed ${name}`)
}, [name])
return (
<div>{message}</div>
);
}
export default App;
英文:
You need to understand 3 concepts here.
First is reusable components. When you write something like this in App
<Greet name="Eric" />
<Greet name="Stan" />
<Greet name="Butters" />
It will be similar to this in html
<div>Eric</div>
<div>Stan</div>
<div>Butters</div>
So, in the App component you call the Greet 3 times. It created 3 instances of Greet component
Secondly is useEffect,
When you provide an empty dependency array []
as the second argument to useEffect
, it means that the effect will only run once, after the initial render. It won't run again when the component re-renders.
In your case, you have 3 instances of Greet component inside App component, it run 1 time in 3 instances, so you see 3 console.log display.
Thirdly is how React re-render when something change. Basically, React need something to notify it that the data has changed, and it need to re-render the component accordingly. One simple way is use useState for the data.
So the example will be something like
import React, { useState } from "react";
function App() {
const [name, setName] = useState("Eric");
return (
<>
<Greet name={name} />
<button onClick={() => setName('Stan')}>Change to Stan</button>
<button onClick={() => setName('Butters')}>Change to Butters</button>
</>
);
}
function Greet({ name }) {
const message = `Hello, ${name}!`;
useEffect(() => {
console.log(`1st name ${name}`); // Side-effect!
}, []);
useEffect(() => {
console.log(`name changed ${name}`)
}, [name])
return (
<div>{message}</div>
);
}
export default App;
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论