【发布时间】:2019-08-09 22:51:30
【问题描述】:
在react hooks中,这3个sn-ps有什么区别。
//1.
function App() {
const [isOn, setIsOn] = useState(false);
useEffect(() => {
const interval = setInterval(() => console.log('tick'), 1000);
return () => clearInterval(interval);
});
}
//2.
function App() {
const [isOn, setIsOn] = useState(false);
useEffect(() => {
const interval = setInterval(() => console.log('tick'), 1000);
return () => clearInterval(interval);
}, []);
}
//3.
function App() {
const [isOn, setIsOn] = useState(false);
useEffect(() => {
const interval = setInterval(() => console.log('tick'), 1000);
return () => clearInterval(interval);
}, [isOn]);
}
传递空数组、带元素的数组和根本不传递之间的区别?
【问题讨论】:
-
那不是第二个参数吗?
-
只需查看文档reactjs.org/docs/… 之间的区别就像“总是”、“一次”和“每次更改为
isOn”之后都一样
标签: reactjs react-hooks