简体   繁体   中英

Where to use Usestate hook in react component

I am trying to use UseState hook in react but I am not sure where to define it. I tried to declare it in React component but compiler gives an error

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

Above code throws an error saying identifier requires.

在此处输入图像描述

You cannot use useState in a class component. You need a functional component.

Here is the doc

Here is the code for your example:

import React, { useState } from 'react';

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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