[React] useEffect - problem: dependency is Object
Let's say we have a simple app look like this:
import { useEffect, useState } from "react";
function useFetch(config) {
console.log("useFetch call");
const [data, setData] = useState(null);
useEffect(() => {
if (config.url) {
fetch(config.url)
.then((response) => response.json())
.then((json) => {
setData(json);
});
}
}, [config]);
return { data };
}
export default function App() {
const [url, setUrl] = useState(null);
const { data } = useFetch({ url });
return (
<div className="App">
<div>Hello</div>
<div>{JSON.stringify(data)}</div>
<div>
<button onClick={() => setUrl("/jack.json")}>Jack</button>
<button onClick={() => setUrl("/jelly.json")}>Sally</button>
</div>
</div>
);
}
It has a useFetch
custome hook, when any button clicked, it will be invoked because url
changes will trigger `useFetch` re-run.
const [url, setUrl] = useState(null);
const { data } = useFetch({ url });
But the problem here is that, it get stuck in infinity loop:
The reason is we passing {url}
to useFetch
, and inside useFetch
, there is useEffect
which deps on {url}
.
Solution 1: instead passing object, just pass primitve value:
function useFetch(config) {
console.log("useFetch call");
const [data, setData] = useState(null);
useEffect(() => {
if (config.url) {
fetch(config.url)
.then((response) => response.json())
.then((json) => {
setData(json);
});
}
}, [config.url]); // using config.url instead of config object
return { data };
}
But what if we have also callback function
inside config
object?
export default function App() {
const [url, setUrl] = useState(null);
const onSuccess = () => console.log("success");
const { data } = useFetch({ url, callback: onSuccess });
...
function useFetch(config) {
console.log("useFetch call");
const [data, setData] = useState(null);
useEffect(() => {
if (config.url) {
fetch(config.url)
.then((response) => response.json())
.then((json) => {
setData(json);
config.callback();
});
}
}, [config.url, config.callback]); // add callback as deps
return { data };
}
Now again, it become crazy.
Solution to the callback problem, we can use useRef
to resolve it:
function useFetch(config) {
console.log("useFetch call");
const [data, setData] = useState(null);
const onSuccessRef = useRef(config.callback);
useEffect(() => {
if (config.url) {
fetch(config.url)
.then((response) => response.json())
.then((json) => {
setData(json);
onSuccessRef.current?.();
});
}
}, [config.url]);
return { data };
}
This solution doesn't work if callback changed... so we need to do
import { useEffect, useRef, useState, useLayoutEffect } from "react";
function useFetch(config) {
const [data, setData] = useState(null);
const onSuccessRef = useRef(config.callback);
useLayoutEffect(() => {
onSuccessRef.current = config.callback
}, [config.callback])
useEffect(() => {
if (config.url) {
fetch(config.url)
.then((response) => response.json())
.then((json) => {
setData(json);
onSuccessRef.current?.();
});
}
}, [config.url]);
return { data };
}
We use useLayoutEffect
to keep ref callback up to date.
To improve, we can create a helper function:
import { useEffect, useRef, useState, useLayoutEffect } from "react";
function useCallbackRef(callback) {
const callbackRef = useRef(callback);
useLayoutEffect(() => {
callbackRef.current = callback;
}, [callback]);
return callbackRef;
}
function useFetch(config) {
const [data, setData] = useState(null);
const savedOnSuccess = useCallbackRef(config.callback).current;
useEffect(() => {
if (config.url) {
fetch(config.url)
.then((response) => response.json())
.then((json) => {
setData(json);
savedOnSuccess();
});
}
}, [config.url]);
return { data };
}
Add cancellation to the useEffect:
import { useEffect, useRef, useState, useLayoutEffect } from "react";
function useCallbackRef(callback) {
const callbackRef = useRef(callback);
useLayoutEffect(() => {
callbackRef.current = callback;
}, [callback]);
return callbackRef;
}
function useFetch(config) {
const [data, setData] = useState(null);
const savedOnSuccess = useCallbackRef(config.callback).current;
useEffect(() => {
let isCancelled = false;
if (config.url) {
fetch(config.url)
.then((response) => response.json())
.then((json) => {
if (!isCancelled) {
setData(json);
savedOnSuccess();
}
});
}
return () => {
isCancelled = true;
};
}, [config.url]);
return { data };
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· AI技术革命,工作效率10个最佳AI工具
2020-10-26 [CSS] Use CSS Transforms to Create Configurable 3D Cuboids
2020-10-26 [CSS] Use CSS Variables Almost like Boolean Values with Calc (maintainable css)
2020-10-26 [Kotlin] Typecheck with 'is' keyword, 'as' keyword for assert type
2020-10-26 [Kotlin] When to add () and when not to
2020-10-26 [Kotlin] fold / reduce
2016-10-26 [CSS] Control Image Aspect Ratio Using CSS (object-fit)
2016-10-26 [Angular2 Form] Reactive form: valueChanges, update data model only when form is valid