简体   繁体   English

如何从基于 class 的组件中的反应 function 调用返回的 const

[英]How to call returned const from a react function in a class based component

Here I have a Loading screen as a functional react component that I try to render conditionally in the App component.在这里,我有一个加载屏幕作为功能性反应组件,我尝试在 App 组件中有条件地呈现。 The concept of this loading screen is that I have a boolean variable that will be used to conditionally render the home page after the loading screen ends.这个加载屏幕的概念是我有一个 boolean 变量,它将用于在加载屏幕结束后有条件地呈现主页。

 import React from 'react'; import { useState, useEffect } from 'react'; import { useSpring, animated } from 'react-spring'; import BarLoader from 'react-spinners/BarLoader'; import Logo from "../assets/images/logo.svg"; const LoadingScreen = () => { const spinner = ` display: block; margin: 0 auto; width: 150px; height: 2.5px; `; const style = useSpring({opacity: 1, from: {opacity: 0}}); const [isLoading, setIsLoading] = useState(false); useEffect(() => { setIsLoading(true); setTimeout(() => { setIsLoading(false) }, 4000) }, []) const LoadingTemplate = () => { <animated.div className="loading-screen" style={style}> <div className="loader-wrapper"> <img className="splash-logo" src={Logo} alt="Marouane Edghoughi" /> <BarLoader color="#384BEB" css={ spinner } loading={isLoading} /> </div> </animated.div> } return { LoadingTemplate, isLoading } } export default LoadingScreen;

When I try to call the boolean variable and the screen template in the following code:当我尝试在以下代码中调用 boolean 变量和屏幕模板时:

 render() { const {LoadingTemplate, isLoading} = LoadingScreen(); return ( <Router> { isLoading? <LoadingTemplate />: <Container className="theme p-0" fluid={true}> {/* something will be displayed here */} </Container> } </Router> ); } }

I just get this error:我只是得到这个错误:

Error: Invalid hook call.错误:无效的挂钩调用。 Hooks can only be called inside of the body of a function component.钩子只能在 function 组件的主体内部调用。 This could happen for one of the following reasons:这可能由于以下原因之一而发生:

  1. You might have mismatching versions of React and the renderer (such as React DOM)你可能有不匹配的 React 版本和渲染器(例如 React DOM)
  2. You might be breaking the Rules of Hooks您可能违反了 Hooks 规则
  3. You might have more than one copy of React in the same app See https://reactjs.org/link/invalid-hook-call for tips about how to debug and fix this problem.您可能在同一个应用程序中拥有多个 React 副本,请参阅https://reactjs.org/link/invalid-hook-call以获取有关如何调试和修复此问题的提示。

The function is working properly if I try to call it from a functional component.如果我尝试从功能组件调用 function,则它工作正常。 This is my first time trying it with a class.这是我第一次尝试使用 class。

Any help would be greatly appreciated ^_^任何帮助将不胜感激^_^

The error message is 100% correct.错误消息是 100% 正确的。 Hooks can be used only in Function Components, and cannot be used like this in class components.挂钩只能在 Function 组件中使用,不能在 class 组件中这样使用。 The underlying mechanics of the two types are different.这两种类型的基本机制是不同的。 Hooks are a feature of functional components and rely on those mechanics, and are therefore not compatible with class components.钩子是功能组件的一个特性,依赖于这些机制,因此与 class 组件不兼容。

You may not realize that you are using a hook, but LoadingScreen actually is one: It returns a value other than a React Element, and you are calling it as a function (ie const x = LoadingScreen() ), rather than using it as a component (ie <LoadingScreen /> ).您可能没有意识到您正在使用钩子,但LoadingScreen实际上是一个:它返回一个不是 React 元素的值,并且您将其称为 function (即const x = LoadingScreen() ),而不是将其用作一个组件(即<LoadingScreen /> )。

That's definitely not allowed in class components.在 class 组件中绝对不允许这样做。 You could use a function component instead:您可以改用 function 组件:

const Component = () => {

    const {LoadingTemplate, isLoading} = LoadingScreen();

    return (
      <Router>
        {
        isLoading ?

            <LoadingTemplate />

          :

          <Container className="theme p-0" fluid={true}>
            {/* something will be displayed here */}
          </Container>

        }
        
      </Router>
    );
  }
}

Or you can try these methods to get around this limitation.或者您可以尝试这些方法来解决此限制。 If you do decide to use a function component instead, then you should use useLoadingScreen to follow the React hook naming conventions.如果你决定改用 function 组件,那么你应该使用useLoadingScreen来遵循 React hook 命名约定。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM