【问题标题】:ways of exporting the default function app with code inside in react native在 react native 中导出带有代码的默认功能应用程序的方法
【发布时间】:2021-09-23 00:28:56
【问题描述】:

我是 react native 的新手,我有一个关于函数导出的问题,特别是默认应用程序的导出。

我见过两种导出方式,第一种是:

export default function App() {
  return (/*Code goes in here and it renders*/);
}

自动渲染里面的任何代码,另一个是通过创建一个类组件,像这样

class App extends Component {
 /*functions like componentDidMount() can go in here/*

 render() { /*now the rendering code goes in here}
}

export default App;

这两种导出默认应用的方式有什么区别?是不是第一个只能返回基本代码,而第二个可以定义状态并创建像componentDidMount()这样的函数?或者你可以在两者中做同样的事情,但这只是一种声明方式?

感谢您在解释此事时花费时间和考虑。

【问题讨论】:

标签: react-native function components


【解决方案1】:

您正在定义不同类型的组件。

第一个是函数组件,用函数定义。

第二个是类组件,用一个类来定义。

有关它们之间差异的更多信息,您可以查看https://www.geeksforgeeks.org/differences-between-functional-components-and-class-components-in-react/ 或谷歌搜索。

【讨论】:

    猜你喜欢
    • 2021-05-20
    • 2018-07-05
    • 2020-05-21
    • 2019-05-08
    • 2020-04-08
    • 2022-01-25
    • 1970-01-01
    • 2020-05-30
    • 2020-12-14
    相关资源
    最近更新 更多