简体   繁体   English

React:从 useEffect 调用自定义钩子或在组件挂载之前运行代码

[英]React: call custom hook from useEffect or run code before component mounts

I'm a bit reluctant to even ask this but here goes...我什至有点不愿意问这个,但是这里......

I have a custom hook useScript based off this implementation but I want to call it from useEffect so that it runs before the component mounts but according to the rules of hooks I know I can't do that.我有一个基于此实现的自定义钩子 useScript,但我想从useEffect调用它,以便它在组件安装之前运行,但根据钩子的规则,我知道我不能这样做。

The issue is that I want to delay rendering my component until the script has loaded but I have no way to set state without causing a 'Too many re-renders' Error.问题是我想延迟渲染我的组件,直到脚本加载,但我无法设置 state 而不会导致“重新渲染过多”错误。

Here are two options I have tried that do NOT work:这是我尝试过的两个不起作用的选项:

  useEffect(() => {
    // Cannot call another hook from inside useEffect
    useScript('https://cdnjs.cloudflare.com/ajax/libs/typescript/3.9.7/typescript.min.js');
  }, []);

and

  const myComponent = (scripts) => {

    const [loaded, setLoaded] = useState(false);

    scripts.forEach(script => {
      useScript('https://cdnjs.cloudflare.com/ajax/libs/typescript/3.9.7/typescript.min.js');
    });

    // where can I call setLoaded ??

    return (loaded && <div>some content</div>);
  };

This should be simple;这应该很简单; What am I forgetting???我忘了什么???

One approach is for a hook to return a function that executes some functionality.一种方法是让钩子返回执行某些功能的 function。

So instead of:所以而不是:

function useScript() {
  const [isLoaded, setIsLoaded] = useState(false);

  // load the script...

  return [isLoaded]
}

You can do:你可以做:

function useLoadScript() {
  const [isLoaded, setIsLoaded] = useState(false);

  const loadScript = () => {
    // load the script...
  }

  return [isLoaded, loadScript]
}

So that the functionality can be consumed within another hook:以便可以在另一个钩子中使用该功能:

function MyComponent(props) {
  const [isLoaded, loadScript] = useLoadScript()
  
  useEffect(() => {
    loadScript()
    // ...
  }, [])

  if (!isLoaded) {
    return <p>Loading...</p>
  }

  return (
    // ...
  )
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 每次安装组件时,React hook useEffect 都会导致初始渲染 - React hook useEffect causes initial render every time a component mounts 在渲染组件之前运行 useEffect 钩子 - Make useEffect hook run before rendering the component 如何将 useEffect 从组件移动到自定义挂钩 - How to move useEffect from component to custom hook React:useEffect是否保证在组件呈现之前运行? - React: Is useEffect not guaranteed to run before the component renders? 使用 React,如何将 function 从父组件传递给子组件并在 useEffect 挂钩中运行? - Using React, how do you pass a function from a parent component to a child component and run it in the useEffect hook? 我如何在 React 中使用 useEffect 仅在组件首次挂载时运行一些代码,而在事件发生时(重复)运行一些其他代码? - How can I use useEffect in React to run some code only when the component first mounts, and some other code whenever an event occurs (repeatedly)? 在 React 的 useEffect() 挂钩中使用 setInterval() 之前尝试进行 API 调用 - Trying to make an API call before using setInterval() in useEffect() hook in React 如何使用 useEffect 钩子和异步调用编写 React 组件单元测试? - How to write a React Component unit test with useEffect hook and async call? 反应:暂停的组件不运行 useEffect 钩子 - React: Suspended component doesn't run the useEffect hook 带有useEffect的自定义react钩子,不能在非组件函数中使用 - Custom react hook with useeffect, cant use in non-component function
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM