Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
96 views
in Technique[技术] by (71.8m points)

javascript - React Typescript FC

What the difference between the code below? All of them works fine but I wanna know which use correctly

import * as React from 'react';

type Props = {...some props..};

const App: React.FC<Props> = props => {
  return (...some content...)
}

and

import React from 'react';

type Props = {...some props..};

const App: React.FC<Props> = props => {
  return (...some content...)
}

and

import React, { FC } from 'react';

type Props = {...some props..};

const App: FC<Props> = props => {
  return (...some content...)
}

Which variant wrong or right?

question from:https://stackoverflow.com/questions/65916448/react-typescript-fc

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Mainly difference is the way you do the imports.

  1. You import React object and assign an alias to it as 'React'. After that you use the "property" FC of the aliased import
  2. You import React and you use the "property" FC of the import
  3. You import React and then you import the named export FC which is the one you use later.

In the three cases you are importing the default exported from the module 'react' as React and in the third you import also the named export FC.

There is no just one valid way to do it and the result is the same.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...