最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

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

programmeradmin8浏览0评论

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)

发布评论

评论列表(0)

  1. 暂无评论