【发布时间】:2021-03-19 13:54:04
【问题描述】:
我正在尝试在我的类组件之外在 react native 中创建以下常量变量:
import { Component, useState } from 'react';
const [isEnabled, setIsEnabled] = useState(false);
const toggleSwitch = () => setIsEnabled(previousState => !previousState);
class NotificationScreen extends Component {
但是运行app时会弹出如下错误:
Invariant Violation: 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:
1. You might have mismatching versions of React and the renderer (such as React DOM)
2. You might be breaking the Rules of Hooks
3. You might...
有没有办法在类中使用常量变量?
【问题讨论】:
-
你违反了钩子的规则。 React 钩子仅在功能组件或其他自定义反应钩子中有效。 React 钩子与
const变量声明无关,这些是标准的 javascript。它们几乎可以在任何地方声明。您要解决的问题是什么?
标签: reactjs react-native class react-hooks constants