繁体   English   中英

在 React 组件中哪里使用 Usestate 钩子

[英]Where to use Usestate hook in react component

我试图在反应中使用 UseState 钩子,但我不确定在哪里定义它。 我试图在 React 组件中声明它,但编译器给出了错误

 class VoiceCallComponent extends React.Component { const [val, setVal] = React.useState(7); .. .. .. }

上面的代码抛出一个错误,指出标识符需要。

在此处输入图像描述

您不能在 class 组件中使用useState 您需要一个功能组件。

这是文档

这是您的示例的代码:

import React, { useState } from 'react';

function VoiceCallComponent() {
  const [val, setVal] = useState(7);

  // rest of the component logic goes here
  return (
    // JSX
  )
}

暂无
暂无

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

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