问题
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