简体   繁体   English

关于 app.js 主类/函数的 React-native 问题

[英]React-native question about app.js main class/function

I'm a beginner in RN and I have only basic JS knowledge, but I need to make a small project in React Native.我是 RN 的初学者,我只有基本的 JS 知识,但我需要在 React Native 中制作一个小项目。 I've bought a tutorial but I have problem on start.我已经购买了一个教程,但我在开始时遇到了问题。

On the RN wiki, hello world is written like that:在 RN wiki 上,hello world 是这样写的:

export default class HelloWorldApp extends Component {
      render() {
        return (
          <View style={{ flex: 1, justifyContent: "center", alignItems: "center" }}>
            <Text>Hello, world!</Text>
          </View>
        );
      }
    }

but instructor in my tutorial does like this:但是我的教程中的讲师是这样的:

  export default function App() {
   return (
          <Text>Hello, world!</Text>
       );
      }
    }

The result is similar.结果是相似的。 I understand render() is inner class function and works like default function.我知道 render() 是内部类函数,并且像默认函数一样工作。 Is that correct understanding?这是正确的理解吗? If it is, why some people use that and other doesn't.如果是,为什么有些人使用它而其他人不使用。 What is difference between these solutions?这些解决方案有什么区别? Does it unlock some options which I don't understand yet?它会解锁一些我还不明白的选项吗?

This is a difference between class (1st option) and functional components (2nd in your example).这是(第一个选项)和功能组件(示例中的第二个)之间的区别。 This is basically up to a programmer to choose the style he likes mosts, with both having benefits and downsides.这基本上取决于程序员选择他最喜欢的风格,有利有弊。

If you're new to React, I'd suggest just using any one style that's easier for you to understand.如果您是 React 的新手,我建议您使用任何一种您更容易理解的样式。

If you want to know the difference in more details, here's a good article about it .如果您想了解更多细节的差异,这里有一篇关于它的好文章

JavaScript is a great language that has taken the object oriented programming concept of 'Abstraction' to the next level. JavaScript 是一种很棒的语言,它将“抽象”的面向对象编程概念提升到了一个新的水平。

In Javascript, everything is an object!在 Javascript 中,一切都是对象!

Even a function can be abstracted into an object.甚至一个函数也可以抽象为一个对象。

The statement 'export default function App()' will export the function named App as an object named 'App'.语句“export default function App()”将把名为 App 的函数导出为名为“App”的对象。

This will make it possible to use the App function the same way like a regular object of class (HelloWorldApp)!这将使您可以像使用类的常规对象 (HelloWorldApp) 一样使用 App 函数!

Abstracting a function like an object is a very useful feature.像对象一样抽象函数是一个非常有用的特性。 It only takes the new learners a little bit of getting used to the idea!新的学习者只需要一点点习惯这个想法!

with classes (your fist exmp.) you can often find approaches from tutorials you find around internet.通过课程(你的第一个例子),你通常可以从你在互联网上找到的教程中找到方法。 Goodness for the beginners are lifecycles used in class.初学者的好处是课堂上使用的lifecycles In the other hand is luck the functionality as typical function (read lines and functional re-render) therefore your code will be soon confusing.另一方面是幸运的功能作为典型功能(读取行和功能重新渲染)因此您的代码很快就会混乱。 Again in functions (your second exmp.) you will have to use hooks let say useState , setState etc.再次在函数中(你的第二个例子)你将不得不使用hooks比如useStatesetState等。

If I can suggest to you, continue with your tutorial and learn function scripting, is more valuable and will lead you to learn redux as well (may be) :).如果我可以向您建议,继续您的教程并学习函数脚本,更有价值,并将引导您学习redux (可能是):)。

Happy codding快乐编码

In first class you can add no.在头等舱中,您可以添加 no。 of components,but in second class you have to add an outer view to add more components.组件,但在第二类中,您必须添加外部视图以添加更多组件。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM