javascript - What does `import type {Node} from 'react';` and usage in App: () => Node - Stack Overflow

Running this mand:npx react-native init AwesomeProjectIn the App.js file I don't understand 2 lin

Running this mand:

npx react-native init AwesomeProject

In the App.js file I don't understand 2 lines:

import React from 'react';
import type {Node} from 'react';  // 1
import {
  SafeAreaView,
  ScrollView,
 // ..... Code .... 

const App: () => Node = () => {  // 2
   // ..... Code .... 


export default App;
  1. Importing type Node

    Following Is there a point to doing 'import type' rather than 'import' with Flow?, I understand that such import is used to import a type of object, for instance:

    import type { Array, Object, ... } from 'wherever';
    

    To be honest I am more concerned on the next point (probably If I understand that I would automatically get the this as well).

  2. const App: () => Node = () =>

    All that I see is that App is a variable which references a function that returns an Object of type Node which this Object s also a function. Does it wrap the App into a 'React' instance or something?

Running this mand:

npx react-native init AwesomeProject

In the App.js file I don't understand 2 lines:

import React from 'react';
import type {Node} from 'react';  // 1
import {
  SafeAreaView,
  ScrollView,
 // ..... Code .... 

const App: () => Node = () => {  // 2
   // ..... Code .... 


export default App;
  1. Importing type Node

    Following Is there a point to doing 'import type' rather than 'import' with Flow?, I understand that such import is used to import a type of object, for instance:

    import type { Array, Object, ... } from 'wherever';
    

    To be honest I am more concerned on the next point (probably If I understand that I would automatically get the this as well).

  2. const App: () => Node = () =>

    All that I see is that App is a variable which references a function that returns an Object of type Node which this Object s also a function. Does it wrap the App into a 'React' instance or something?

Share Improve this question edited Jul 9, 2021 at 13:38 jonrsharpe 122k30 gold badges268 silver badges475 bronze badges asked Jul 9, 2021 at 13:28 Federico BaùFederico Baù 7,7755 gold badges41 silver badges48 bronze badges 1
  • 1 () => Node is the type. – jonrsharpe Commented Jul 9, 2021 at 13:34
Add a ment  | 

1 Answer 1

Reset to default 4

What does const App: () => Node = () => do and why you would want to use it?

If we remove the types, the code is:

const App = () => {
  // ... code
}

This is a react functional ponent.

Then on top of that is added a type: () => Node. This means it's a function that takes no parameters and returns a react Node. Types help with development by letting the puter analyze the code better, and point out bugs sooner (before even running the code)

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744272352a4566162.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信