A brief discussion on the use of React.FC and React.Component

A brief discussion on the use of React.FC and React.Component

React components can be defined as functions (React.FC<>) or classes (inheriting React.Component).

1. React.FC<>

1. React.FC is a functional component, a generic used in TypeScript. FC is the abbreviation of FunctionComponent. In fact, React.FC can be written as React.FunctionComponent:

const App: React.FunctionComponent<{ message: string }> = ({ message }) => (
  <div>{message}</div>
);

2. React.FC includes PropsWithChildren generics, so you don’t need to explicitly declare the type of props.children. React.FC<> is explicit about the return type, whereas the normal function version is implicit (and would require an additional annotation otherwise).

3. React.FC provides type checking and auto-completion of static properties: displayName, propTypes, and defaultProps (Note: there are some issues with using defaultProps in conjunction with React.FC).

4. When we use React.FC to write React components, we cannot use setState. Instead, we use Hook APIs such as useState() and useEffect.

Example (Ali's Ant Desgin Pro framework is used here for demonstration):

const SampleModel: React.FC<{}> = () =>{ //React.FC<> is a generic type used by typescript const [createModalVisible, handleModalVisible] = useState<boolean>(false); 
   return {
   {/** Trigger modal box **/}
   <Button style={{fontSize:'25px'}} onClick={()=>handleModalVisible(true)} >Example</Button>
   {/** Modal box component **/}
   <Model onCancel={() => handleModalVisible(false)} ModalVisible={createModalVisible} /> 
  }

2. class xx extends React.Component

If you want to define a class component, you need to inherit React.Component. React.Component is a class component. In TypeScript, React.Component is a generic type (aka React.Component<PropType, StateType>), so you provide it with (optional) prop and state type parameters:

Example (Ali's Ant Desgin Pro framework is used here for demonstration):

class SampleModel extends React.Component {
  state = {
    createModalVisible:false,
  };

  handleModalVisible =(cVisible:boolean)=>{
    this.setState({createModalVisible:cVisible});
  };
  return {
  {/** Trigger modal box **/}
   <Button onClick={()=>this.handleModalVisible(true)} >Example</Button>
   {/** Modal box component **/}
   <Model onCancel={() => handleModalVisible(false)} ModalVisible={this.state.createModalVisible} /> 
  }

ps: Simply put, when you don’t know what component type to use, use React.FC.

This concludes this article on the use of React.FC and React.Component in React. For more content about React.FC and React.Component in React, please search for previous articles on 123WORDPRESS.COM or continue to browse the following related articles. I hope you will support 123WORDPRESS.COM in the future!

You may also be interested in:
  • A brief discussion on React Component life cycle functions
  • A brief discussion of several ways to create React Component
  • Detailed explanation of several forms of React Component
  • React Stateless Component and Higher-Order Component
  • React styled-components method to set component properties
  • Detailed explanation of the importance and usage of PureComponent in React.js

<<:  Detailed deployment of Alibaba Cloud Server (graphic tutorial)

>>:  Detailed explanation of MySQL startup options and system variables examples

Recommend

Detailed explanation of the working principle and solution of Js modularization

Table of contents 1. Modular concept 2. Modulariz...

In-depth explanation of modes and environment variables in Vue CLI

Preface In the development of actual projects, we...

Detailed explanation of Vue slot

1. Function : Allows the parent component to inse...

Analysis of the principle and usage of MySQL continuous aggregation

This article uses examples to illustrate the prin...

Solutions to the Problem of Creating XHTML and CSS Web Pages

The solutions to the problems encountered during x...

Detailed example of using case statement in MySQL stored procedure

This article uses an example to illustrate the us...

Analysis of the HTML writing style and reasons of experienced people

1. Navigation: Unordered List vs. Other Label Ele...

How to use shell to perform batch operations on multiple servers

Table of contents SSH protocol SSH Connection pro...

Solve the problem of MySQL Threads_running surge and slow query

Table of contents background Problem Description ...

How to implement mobile web page size adaptation

I finally finished the project at hand, and the m...

Explanation of nginx load balancing and reverse proxy

Table of contents Load Balancing Load balancing c...

WeChat applet realizes the effect of shaking the sieve

This article shares the specific code of the WeCh...

Vue uses Echarts to implement a three-dimensional bar chart

This article shares the specific code of Vue usin...

Best Practices for Sharing React Code

When any project develops to a certain complexity...

How to use multi-core CPU to speed up your Linux commands (GNU Parallel)

Have you ever had the need to compute a very larg...