【问题标题】:React Native functional component constructorReact Native 功能组件构造器
【发布时间】:2022-11-22 01:15:24
【问题描述】:

怎么才能转换类组件 构造函数功能组件

来自类组件

constructor(props) {
     super(props);
      this.state = {
       myText: 'I\'m ready to get swiped!',
       gestureName: 'none',
       backgroundColor: '#fff'
      };
    }

功能组件

const Component = () => {
//CODE
}

【问题讨论】:

    标签: react-native


    【解决方案1】:

    要将类更改为函数,您必须知道什么是hooks:那么什么是hooks?:它们让您无需编写类即可使用状态和其他 React 功能。

    定义你将使用useState函数的状态(useState it one kooks)

    所以你的派系组件将如下所示:

    import React, {useState} from 'react';
    const Component = () => {
      const [myText, setMyText] = useState('I'm ready to get swiped!');
      const [gestureName, setGestureName] = useState('none');
      const [backgroundColor, setBackgroundColor] = useState('#fff');
    
      return ....
    }
    

    要了解有关功能组件及其挂钩的更多信息,请查看他们的文档: https://reactjs.org/docs/hooks-intro.html

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-11-01
      • 2021-01-30
      • 2020-05-30
      • 1970-01-01
      • 2021-06-14
      • 2022-01-14
      • 2021-01-08
      • 1970-01-01
      相关资源
      最近更新 更多