简体   繁体   English

React native:如何将 class 组件代码转换为带有钩子的 function 组件?

[英]React native : How can I turn the class component code into a function component with hooks?

How can I turn the following code into a function component with hooks?如何将以下代码转换为带有钩子的 function 组件? In my example I use a class component And I want to change the code to a function component form在我的示例中,我使用 class 组件并且我想将代码更改为 function 组件形式

export default class Modal2 extends Component {
  state = {
    placeName: "",
    errorMsg: null
  };

placeNameChangedHandler = val => {
    this.setState({
      placeName: val,
      errorMsg: null
    });
  };

onConfirm = () => {
    const { placeName } = this.state;
    const { onConfirm, onHideModal } = this.props;
    if (placeName.trim().length > 5) {
      onConfirm("Cancel", placeName);
      onHideModal();
      this.setState({ placeName: "", errorMsg: null })
    } else {
      this.setState({ errorMsg: "must 5 letters" });
    }
  };
}

That's how it should look after converting it.这就是转换后的样子。

import React, { useState } from 'react';

function Modal2(props) {
 const [desiredStateName, setDesiredStateName] = useState({
    placeName: "",
    errorMsg: null
  });


placeNameChangedHandler = val => {
  setDesiredStateName({
      placeName: val,
      errorMsg: null
    });
  };

onConfirm = () => {
    const { placeName } = desiredStateName.placeName;
    const { onConfirm, onHideModal } = props;
    if (placeName.trim().length > 5) {
      onConfirm("Cancel", placeName);
      onHideModal();
      setDesiredStateName({ placeName: "", errorMsg: null })
    } else {
     setDesiredStateName((prevState)=>{
        return{ ...prevState,errorMsg: "must 5 letters" }
    })
  }
  };
}
export default Modal2;

Also, a quick guide of how you are able to do it by yourself此外,快速指南,您如何能够自己做到这一点

Have a try by replacing your code with the below code:尝试用以下代码替换您的代码:

import React, { useState } from 'react';

export default Modal2 = props => {

    const [placeName, setPlaceName] = useState("")
    const [errorMsg, setErrorMsg] = useState(null)

    placeNameChangedHandler = val => {
        setPlaceName(val)
        setErrorMsg(null)
    };

    onConfirm = () => {
        const { onConfirm, onHideModal } = props;
        if (placeName.trim().length > 5) {
            onConfirm("Cancel", placeName);
            onHideModal();
            setPlaceName("")
            setErrorMsg(null)
        } else {
            setErrorMsg("must 5 letters")
        }
    };
}

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

相关问题 REACT NATIVE:如何将示例代码更改为带有钩子的 function 组件? - REACT NATIVE: How can I change the example code to function component with hooks? 我怎样才能把这个 React class 组件变成一个功能组件? - How can i turn this React class component into a functional component? react native:将类组件转换为带有钩子的函数组件的方法是什么? - react native: what is the way to transform the class component to a function component with hooks? react native:将类组件更改为函数组件和钩子的方法是什么? - react native : what is the way to change the class component to a function component and hooks? 如何在本机反应中将无状态 function 转换为 class 组件 - How can I convert stateless function to class component in react native 我如何在 React 经典的 `class` 组件中使用 React hooks? - How can I use React hooks in React classic `class` component? 如何将此 Class 组件 function 转换为 React Native 中的功能组件 - How can I convert this Class component function into functional component in React Native react native hooks 只能在函数组件的主体内部调用 - react native hooks can only be called inside body of a function component 在本机反应中将组件代码转换为钩子 - Converting component code to hooks in react native 如何在 class 组件中使用导航? REACT-NATIVE - How can I use navigation in a class component? REACT-NATIVE
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM