React:如何在自定义钩子中重置状态

huangapple go评论59阅读模式
英文:

React: How to reset state in custom hook

问题

我有一个React组件(AddSlides),它使用useUpload自定义钩子来上传文件,钩子返回下载链接。当AddSlides组件重新渲染时,钩子返回相同的值。为了避免这种情况,我在AddSlides组件的useEffect中调用reset方法,但这导致无限循环,这并不奇怪。

如何在使用后将downloadUrls重置为空?(请参见下面的reset方法。)

React组件addSlides.js

export default function AddSlides() {
  const [filesToUpload, setFilesToUpload] = useState([]);
  const { downloadUrls, setDownloadUrls, reset } = useUpload(
    filesToUpload,
    "AddSlides"
  );
  const [allUrls, setAllUrls] = useState([]);

  useEffect(() => {
    setAllUrls([...allUrls, ...downloadUrls]);
    setFilesToUpload([]); // 重置文件为空

    //reset(); // 重置下载链接,但这会导致无限循环。
  }, [downloadUrls]);

  return (<>//一些代码</>);
}

自定义钩子useUpload.js

export default function useUpload(files, componentName = "") {
  const [downloadUrls, setDownloadUrls] = useState([]);

  const reset = () => {
    console.log("将下载链接重置为空");
    setDownloadUrls([]); // 将值重置为空数组。
  };

  useEffect(() => {
    // 这里有一些代码
  }, [files, componentName]);

  return {
    downloadUrls,
    setDownloadUrls,
    reset,
  };
}
英文:

I have a react component (AddSlides) which is using useUpload custom hook to upload files, in response the hook returns the download urls. The hook is returning the same value when AddSlides component is re-rendered. To avoid this I am calling reset method in useEffect of AddSlides component, but this is causing infinite loop, which is not surprising.

How do I reset the downloadUrls to empty after using it ? (See below reset method.

React Component addSlides.js

export default function AddSlides() {
  const [filesToUpload, setFilesToUpload] = useState([]);
  const { downloadUrls, setDownloadUrls, reset } = useUpload(
    filesToUpload,
    &quot;AddSlides&quot;
  );
  const [allUrls, setAllUrls] = useState([]);

 useEffect(() =&gt; {
  setAllUrls([...allUrls, ...downloadUrls]);
  setFilesToUpload([]); // Resetting the files to empty

  //reset(); // Resetting the download urls, but this is causing an infinite loop.

}, [downloadUrls]);
return (&lt;&gt;//some code&lt;/&gt;)
}

Custom hook useUpload.js

export default function useUpload(files, componentName = &quot;&quot;) {
  const [downloadUrls, setDownloadUrls] = useState([]);

  const reset = () =&gt; {
    console.log(&quot;Setting download urls to empty&quot;);
    setDownloadUrls([]); // Resetting the value to empty array.
  };

  useEffect(() =&gt; {
   // Some code here
  }, [files, componentName]);

 return {
   downloadUrls,
   setDownloadUrls,
   reset,
 };
}

答案1

得分: 1

只有在数组长度大于0时才执行重置。

英文:

Execute reset only if array length > 0

useEffect(() =&gt; {
  setAllUrls([...allUrls, ...downloadUrls]);
  setFilesToUpload([]); // Resetting the files to empty

  if (downloadUrls.length &gt; 0) {
    reset()
  }

}, [downloadUrls]);

huangapple
  • 本文由 发表于 2023年2月19日 19:58:37
  • 转载请务必保留本文链接:https://go.coder-hub.com/75499983.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定