没有数组的使用效应总是迭代,为什么?

发布于 2025-02-06 13:23:24 字数 1697 浏览 1 评论 0原文

问题:
为什么没有数组的代码总是迭代与仅执行一次的数组相比,与代码相比?

Stackblitz:

没有数组:
https://stackblitz.com/edit/eedit/edit/reaect-ts-ts-9w3cgd? file = app.tsx

带有数组:
https://stackblitz.com/edit/edit/reactt-ts-ts-xrrvan? file = app.tsx

谢谢!


没有

import axios from 'axios';
import * as React from 'react';
import { useEffect, useState } from 'react';
import './style.css';

export default function App() {
  const [data, setData] = useState();

  useEffect(() => {
    axios
      .get('https://jsonplaceholder.typicode.com/users')
      .then((result) => setData(result.data));

    //console.log(data);
    console.log('t');
  });

  return (
    <div>
      <h1>Hello StackBlitz!</h1>
      <p>Start editing to see some magic happen :)</p>
    </div>
  );
}

数组的数组

import axios from 'axios';
import * as React from 'react';
import { useEffect, useState } from 'react';
import './style.css';

export default function App() {
  const [data, setData] = useState();

  useEffect(() => {
    axios
      .get('https://jsonplaceholder.typicode.com/users')
      .then((result) => setData(result.data));

    console.log(data);
    //console.log("t");
  }, []);

  return (
    <div>
      <h1>Hello StackBlitz!</h1>
      <p>Start editing to see some magic happen :)</p>
    </div>
  );
}

Question:
WHY does the code without array always iterate in relation compare to the code with array that only execute once?

Stackblitz:

Without array:
https://stackblitz.com/edit/react-ts-9w3cgd?file=App.tsx

With array:
https://stackblitz.com/edit/react-ts-xrrvan?file=App.tsx

Thank you!


Without array

import axios from 'axios';
import * as React from 'react';
import { useEffect, useState } from 'react';
import './style.css';

export default function App() {
  const [data, setData] = useState();

  useEffect(() => {
    axios
      .get('https://jsonplaceholder.typicode.com/users')
      .then((result) => setData(result.data));

    //console.log(data);
    console.log('t');
  });

  return (
    <div>
      <h1>Hello StackBlitz!</h1>
      <p>Start editing to see some magic happen :)</p>
    </div>
  );
}

With array

import axios from 'axios';
import * as React from 'react';
import { useEffect, useState } from 'react';
import './style.css';

export default function App() {
  const [data, setData] = useState();

  useEffect(() => {
    axios
      .get('https://jsonplaceholder.typicode.com/users')
      .then((result) => setData(result.data));

    console.log(data);
    //console.log("t");
  }, []);

  return (
    <div>
      <h1>Hello StackBlitz!</h1>
      <p>Start editing to see some magic happen :)</p>
    </div>
  );
}

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

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

发布评论

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

评论(1

生生漫 2025-02-13 13:23:24

来自官方 react Hooks 文档文档:

如果您想运行效果并仅清理一次(在安装和卸载上),则可以将空数组([])作为第二个参数传递。这告诉React,您的效果不取决于道具或状态的任何值,因此它无需重新运行。这不是作为特殊情况来处理的 - 它直接遵循依赖性数组的工作原理。

基本上,使用带有空依赖关系数组的挂钩只能在组件安装时运行一次。

另外,如果我们不包括依赖项数组,则每次零件重新租赁时都会运行回调。
每当其父母组件的状态或状态发生变化时,就会发生。

话虽如此,最好避免使用使用 hook 而无需依赖性数组,因为我们通常希望在更改特定状态时以特定的方式使用它。
以这种方式使用它是有效的,但没有真正建议。

From the official React hooks documentation:

If you want to run an effect and clean it up only once (on mount and unmount), you can pass an empty array ([]) as a second argument. This tells React that your effect doesn’t depend on any values from props or state, so it never needs to re-run. This isn’t handled as a special case — it follows directly from how the dependencies array always works.

Basically, a useEffect hook with an empty dependency array should run only once when the component mounts.

Alternatively, if we were to not include a dependency array, the callback will run every time the component re-renders.
Which occurs whenever there is a change in state or the state of any of it's parent components.

With that said, it's probably better to avoid using the useEffect hook without a dependency array, since we would usually want to use it to act in a specific way upon the change of a specific state.
Using it in such a way is valid, but not really advised.

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