没有数组的使用效应总是迭代,为什么?
问题:
为什么没有数组的代码总是迭代与仅执行一次的数组相比,与代码相比?
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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
来自官方 react Hooks 文档文档:
基本上,
使用
带有空依赖关系数组的挂钩只能在组件安装时运行一次。另外,如果我们不包括依赖项数组,则每次零件重新租赁时都会运行回调。
每当其父母组件的状态或状态发生变化时,就会发生。
话虽如此,最好避免使用
使用
hook 而无需依赖性数组,因为我们通常希望在更改特定状态时以特定的方式使用它。以这种方式使用它是有效的,但没有真正建议。
From the official React hooks documentation:
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.