首页 > 其他分享 >[React] useEffect - problem: dependency is Object

[React] useEffect - problem: dependency is Object

时间:2022-10-26 22:02:45浏览次数:62  
标签:const callback url data React dependency json problem config

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 useFetchcustome hook, when any button clicked, it will be invoked because urlchanges 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 useRefto 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 useLayoutEffectto 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 };
}

 

标签:const,callback,url,data,React,dependency,json,problem,config
From: https://www.cnblogs.com/Answer1215/p/16829497.html

相关文章

  • React进阶篇——六、Diff算法
    六、Diff算法React每次组件的状态或属性更新,组件的render方法都会返回一个新的虚拟DOM对象,用来表述新的UI结构。如果每次render都直接使用新的虚拟DOM来生成真实DOM,那么会......
  • react-json-view
    react-json-viewreact-json-view示例//importthereact-json-viewcomponentimportReactJsonfrom'react-json-view'//usethecomponentinyourapp!<ReactJs......
  • react router6使用
    1.BrowserRouter说明:用于包裹整个应用。importReactfrom"react";importReactDOMfrom"react-dom";import{BrowserRouter}from"react-router-dom";ReactDO......
  • React组件设计模式-纯组件,函数组件,高阶组件
    一、组件(1)函数组件如果你想写的组件只包含一个render方法,并且不包含state,那么使用函数组件就会更简单。我们不需要定义一个继承于React.Component的类,我们可以定......
  • React组件复用的技巧
    复用是组件化开发体系的立命之本,可以说组件化的初衷就是为了复用性。但是组件化的复用方式也存在一定的问题,其中拆分粒度就是其中一个绕不开的话题,今天咱们就来讲一讲Rea......
  • React组件复用的发展史
    MixinsReactMixin通过将共享的方法包装成Mixins方法,然后注入各个组件来实现,官方已经不推荐使用,但仍然可以学习一下,了解为什么被遗弃。ReactMiXin只能通过React.creat......
  • 前端react面试题(边面边更)
    React声明组件有哪几种方法,有什么不同?React声明组件的三种方式:函数式定义的无状态组件ES5原生方式React.createClass定义的组件ES6形式的extendsReact.Component定......
  • 你是如何使用React高阶组件的?
    HighOrderComponent(包装组件,后面简称HOC),是React开发中提高组件复用性的高级技巧。HOC并不是React的API,他是根据React的特性形成的一种开发模式。HOC具体上就是一个接受......
  • 关于 problem.conf
    基本设置problem.conf中一行只能含有一个设置(不然可能会出现奇怪的错误?)use_builtin_judger大多数题的problem.conf里都要有use_builtin_judgeron这句话,这表示您需......
  • React如何更快的完成diff的比较
    diff算法是能够更加高效快捷更新页面元素的算法,那如何帮助diff更快呢?那答案就一定是合理的使用key。diff的调用是在reconcileChildren中的reconcileChildFibers,当没有可以......