为什么这个 useEffect (第一个)不能在依赖项更改时运行?

发布于 2025-01-10 16:02:23 字数 1065 浏览 1 评论 0原文


export default function App() {

  // as the useState runs before useEffect, 
  // it means count is avaliable to use in the useEffect 
  // but why it is not running?
  useEffect(() => {
    console.log("I only run on first render.", count);
  },[count]);

  useEffect(() => {
    console.log("I runs on every render/re-render", count);
  });

  // if i use const in place of var, it throws error of lexical scope
  // but as useState run before the effect the count is already in scope, right?
  var [count, setCount] = useState(0);

  useEffect(() => {
    console.log("run on first render and whenever the count changes", count);
  }, [count]);


  return (
    <div className="App">
      <p>Count: {count}</p>
      <button onClick={() => setCount((prev) => prev + 1)}>count +</button>
    </div>
  );
}

我认为这是第一次渲染的执行顺序:

运行惰性初始化器(或者我们可以说运行 useState)->雷德纳-> dom更新->布瑟油漆->运行效果

对于重新渲染:除了惰性初始化器和运行新效果之前的效果清理之外的所有内容。


export default function App() {

  // as the useState runs before useEffect, 
  // it means count is avaliable to use in the useEffect 
  // but why it is not running?
  useEffect(() => {
    console.log("I only run on first render.", count);
  },[count]);

  useEffect(() => {
    console.log("I runs on every render/re-render", count);
  });

  // if i use const in place of var, it throws error of lexical scope
  // but as useState run before the effect the count is already in scope, right?
  var [count, setCount] = useState(0);

  useEffect(() => {
    console.log("run on first render and whenever the count changes", count);
  }, [count]);


  return (
    <div className="App">
      <p>Count: {count}</p>
      <button onClick={() => setCount((prev) => prev + 1)}>count +</button>
    </div>
  );
}

I think this is the order of execution for first render:

run lazy intializers (or we can say run useState) -> redner -> dom update -> broser paint -> run effect

And for re-render: all the things except lazy intializers and effect cleanup before running new effects.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

剩余の解释 2025-01-17 16:02:23

Eslint 像这样发出警告

var count:数字“count”在定义之前已被使用。
(定义前不使用)eslint

您需要将 useState for count 声明移动到将其用作依赖项的 useEffect 之前。

  var [count, setCount] = useState(0);

  useEffect(() => {
    console.log("I only run on first render.", count);
  }, [count]);

Eslint warns like this

var count: number 'count' was used before it was defined.
(no-use-before-define)eslint

You need to move useState for count declaration before the useEffect which uses it as a dependency.

  var [count, setCount] = useState(0);

  useEffect(() => {
    console.log("I only run on first render.", count);
  }, [count]);
温馨耳语 2025-01-17 16:02:23

您的第一个效果是在定义之前使用 count,因此它始终是 undefined

You're first effect is using count before it has been defined, so it is always undefined.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文