React - Error when using hook inside a function

我们两清 提交于 2021-02-11 18:24:12

问题


When using the npm package "use-dark-mode" I try to apply the hook const darkMode = useDarkMode (false); inside a function but i get an error

Error: Invalid hook call. Hooks can only be called inside of the body of a function component. This could happen for one of the following reasons:

before giving you the code, I want to explain in more detail, I think you already understood from the package name, I want to use dark mode in my project, I have a lot of components, I pass the dark mode value through props and it works fine for me except for one component named JavaScriptLessonObject, I try to call useDarkMode inside this function but it gives an error here is my code. Package link

import useDarkMode from 'use-dark-mode'

function JavaScriptLessonObject() {
    const darkMode = useDarkMode(false)
    return (
        [
            {
                title: [<p className={jsStyle.title}>Overview</p>],
            },

            {
                titleName: "Indorction",
                heading: 'What is JavaScript ?',
                iconName: faDotCircle,
                data: {
                    description: `              
                    <p class="con"> JavaScript is a dynamic computer programming language.
                    It is lightweight and most commonly
                    used as a part of web pages, whose implementations
                    allow client-side script to interact with the user and make dynamic pages.
                    It is an interpreted programming language with object-oriented capabilities.</p>              
                    `
                },
            },
        ]
    );
}

export default JavaScriptLessonObject;

来源:https://stackoverflow.com/questions/65728164/react-error-when-using-hook-inside-a-function

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!