reactjs - 在 react-Native 中创建 UI 时 Class 和 Const 有什么区别?

标签 reactjs react-native react-redux

const App = () => (
  <View>
    <Text>Test</Text>
  </View>
  )

class App extends Component {
  render() {
    return (
      <View>
        <Text>Test</Text>
      </View>
    );
  }
}

当我测试时,两件事是一样的。 请告诉我这两者之间的区别。

最佳答案

类组件是有状态组件,const App 是无状态(或函数式)组件。

有状态组件用于:

  1. 初始化状态
  2. 修改状态
  3. 渲染一些东西

此外,它还有生命周期方法。

而无状态组件通常仅用于返回一段 UI。

简而言之:类组件比函数式组件更强大

编辑:

React Native 0.59功能组件也可以有一个状态。参见 Hooks-Intro了解更多信息。

关于reactjs - 在 react-Native 中创建 UI 时 Class 和 Const 有什么区别?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55965714/

相关文章:

reactjs - 尝试使用 react-redux 分派(dispatch)操作时,类型 'PropsWithChildren' 上不存在 Prop

reactjs - 如何在React中处理多个路由器

ios - React-Native-Webview:期望nodeHandle不为空

React-Native 导航 v5 : How to change a tabBarLabel or tabBarIcon and the colorBackground of the tab on a specific screen?

javascript - React-Native 博览会 POST Blob

javascript - Redux-persist react native AsyncStorage 对象序列化对象不相等

javascript - 在功能组件初始渲染时将变量添加到存储中

reactjs - 未捕获的 TypeError : dispatch(. ..).then 不是一个函数

javascript - VSCode 将逗号分隔值分成行

node.js - 在 ElectronJS 中使用 Electron-Remote 开 Jest 测试 React 组件