1
yena Dec 8, 2023 via iPhone useeffect 默认执行两次
|
2
whj768702 Dec 8, 2023 是严格模式,看看入口文件中是不是有<StrictMode>标签包裹,在开发环境,严格模式下会执行两次。可以参考下文档。https://react.dev/reference/react/StrictMode
|
3
daleqq Dec 8, 2023 via iPhone Development 模式下默认执行两次 mount 提醒你有没有合理的 cleanup.
"To help you spot them quickly, in development React remounts every component once immediately after its initial mount." |
4
potatowish Dec 8, 2023 via iPhone 初学 React 时也遇到过,开发环境下默认是 StrictMode
|
5
realJamespond Dec 8, 2023 ```
export const useMount = (fn: React.EffectCallback) => { const isMounted = useRef(false) useEffect(() => { if (!isMounted.current) { fn() isMounted.current = true } }, []) } ``` 这样就能只执行一次了 |
6
nodejsexpress Dec 8, 2023
好的,看来还是我太菜了。谢谢楼上的各位大佬!
|