简体   繁体   English

如何使用反应钩子将我的 class 和 render() 转换为功能组件?

[英]How do I convert my class and render() to a functional component using react hooks?

I'm trying to convert my class to a functional component but I'm having trouble with the isInputActive .我正在尝试将我的 class 转换为功能组件,但我遇到了isInputActive This is what the class looks like:这就是 class 的样子:

BEFORE

class HelloWorld extends React.Component {
      constructor(props) {
        super(props);
        this.state = {
          isInputActive: false,
        };
      }

render() {
    const focusHandler = () => {
      onChange('');
      this.setState({
        isInputActive: true,
      });
    };

And in my return() , I've got this:在我的return()中,我得到了这个:

<input
  onBlur={() => {
  this.setState({ isInputActive: false });
  }}
 />

So, I tried converting it as seen below:因此,我尝试将其转换为如下所示:

I converted the class to a const:我将 class 转换为 const:

AFTER

const HelloWorld = ({ isInputActive }) => {
  const [isInputActive, setIsInputActive] = useState(false);

Then my render() was converted as seen here:然后我的render()被转换为如下所示:

const focusHandler = () => {
    onChange('');
    setIsInputActive(true);
  };

And finally, I assumed that my <input> in the return() would look like this (the setState became useState which I'm not sure if it's correct either):最后,我假设return()中的<input>看起来像这样( setState变成useState ,我也不确定它是否正确):

onBlur={() => {
 useState({ isInputActive: false });
  }}

Does anyone know what it should look like?有谁知道它应该是什么样子? to make it work without erroring?让它工作而不会出错? Where I've done const PinInput = ({ isInputActive }) => { I get an error: Parsing error: Identifier 'isInputActive' has already been declared我已经完成const PinInput = ({ isInputActive }) => {我得到一个错误: Parsing error: Identifier 'isInputActive' has already been declared

My code might be wrong so you can ignore the AFTER if you want.我的代码可能是错误的,因此您可以根据需要忽略AFTER I'm just trying to make sure I get rid of the render().我只是想确保我摆脱了渲染()。

Change,改变,

onBlur={() => {
 useState({ isInputActive: false });
}}

To,至,

onBlur={() => {
 setIsInputActive(false);
}}

Reason you are getting this error, Parsing error: Identifier 'isInputActive' has already been declared is because, You are already declaring the variable isInputActive here,您收到此错误的原因, Parsing error: Identifier 'isInputActive' has already been declared是因为,您已经在此处声明了变量isInputActive

const [isInputActive, setIsInputActive] = useState(true);

And it already hold the value of true .它已经拥有true的值。

To make it false just do setIsInputActive(false) ..要使其为假,只需执行setIsInputActive(false) ..

You can try replacing your code like this:


import React from "react";

function HelloWorld() {
  
  const [isInputActive, setIsInputActive] = React.useState(true);

  return (
    <>
      <input
        onBlur={() => {
          setIsInputActive(false);
         }}
        />
    </>
  );
}

暂无
暂无

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

相关问题 如何使用 React Hooks 将此级联下拉 class 组件转换为功能组件? - How can I convert the this cascading dropdown class component to functional one using React Hooks? 如何使用 React Hooks 将带有构造函数的 class 转换为功能组件? - How can I convert a class with a constructor to a functional component using React Hooks? 我想使用 React 钩子将我的 React 类组件转换为功能组件。 它给出了如下错误 - I wanted to convert my react class component to a functional component using react hooks. it gave an error like below 如何将 React class 组件转换为带有钩子的功能组件 - How to convert React class component to functional component with hooks 如何将我的 react js 类组件转换为功能组件? - How do I convert my react js class component to a functional component? 如何使用钩子将功能组件转换为类组件 - How to convert functional component using hooks to class component 如何将使用钩子的 React 功能组件转换为类? - How to convert a React Functional Component that uses hooks to a class? 错误:使用钩子将类转换为功能组件 - Error: Convert class to functional component using hooks 如何将具有构造函数和道具的组件转换为使用钩子和道具的功能组件 - How do I convert a component with a constructor function and props, to a functional component using hooks and props *帮助*使用钩子将类转换为功能组件 - *Help* Convert class to functional component using hooks
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM