Summary of React's way of creating components

Summary of React's way of creating components

1. Create components using functions

Functional components: components created using JS functions (or arrow functions)

Convention 1: Function names must start with a capital letter

Convention 2: Function components must have a return value, indicating the structure of the component

If the return value is null, it means nothing is rendered.

function Hello() {
    return (
        <div>This is my first functional component! </div>
    )
}
const Hello = () => <div> This is my first functional component! </div>

Rendering function component: use the function name as the component tag name

Component labels can be single or double

//1. Import react
import React from 'react';
import ReactDOM from 'react-dom';

/*
  Functional components:
*/
function Hello() {
  return (
    <div>This is my first functional component! </div>
  )
}

// Rendering component ReactDOM.render(<Hello />, document.getElementById('root'))

2. Create components using classes

Component class: a component created using ES6 class

Convention 1: Class names must also start with a capital letter

Convention 2: Class components should inherit the React.Component parent class so that they can use the methods or properties provided by the parent class

Convention 3: Class components must provide a render() method

Convention 4: The render() method must have a return value, indicating the structure of the component

//1. Import react
import React from 'react';
import ReactDOM from 'react-dom';

/*
  Functional components:
*/
function Hello() {
  return (
    <div>This is my first functional component! </div>
  )
}

// Rendering component ReactDOM.render(<Hello />, document.getElementById('root'))

3. Extract to independent JS file

1. Create Hello.js

2. Import React in Hello.js

3. Create components (functions or classes)

4. Export the component in Hello.js

5. Import the Hello component in index.js

6. Rendering Components

Hello.js

import React from "react";

//Create component class Hello extends React.Component {
    render () {
        return (
            <div>This is my first component extracted into a js file! </div>
        )
    }
}

//Export component export default Hello

index.js

//1. Import react
import React from 'react';
import ReactDOM from 'react-dom';

/*
  Extract components into separate JS files:
*/

//Import Hello componentimport Hello from './Hello';

// Rendering component ReactDOM.render(<Hello />, document.getElementById('root'))

This concludes this article on two ways to create React components. For more information about how to create React components, please search for previous articles on 123WORDPRESS.COM or continue browsing the following related articles. I hope you will support 123WORDPRESS.COM in the future!

You may also be interested in:
  • How to create components in React
  • React method to create a singleton component
  • Three ways to create components in React and their differences
  • In-depth understanding of the method of creating component this in es6 in React

<<:  Introduction to Semantic XHTML Tags

>>:  Summary of Css methods for clearing floats

Recommend

In-depth understanding of the use of the infer keyword in typescript

Table of contents infer Case: Deepen your underst...

HTML form submission method case study

To summarize the form submission method: 1. Use t...

A quick solution to the first login failure in mysql5.7.20

First, we will introduce how (1) MySQL 5.7 has a ...

Vue+SSM realizes the preview effect of picture upload

The current requirement is: there is a file uploa...

Implementation of vertical centering with unknown height in CSS

This article mainly introduces the implementation...

HTML+CSS+jQuery imitates the search hot list tab effect with screenshots

Copy code The code is as follows: <!DOCTYPE ht...

How to use & and nohup in the background of Linux

When we work in a terminal or console, we may not...

HTML structured implementation method

DIV+css structure Are you learning CSS layout? Sti...

Introduction to several ways to introduce CSS in HTML

Table of contents 1. Embed CSS styles directly in...

Common Linux English Error Chinese Translation (Newbies Must Know)

1.command not found command not found 2. No such ...

JavaScript implements simple calculator function

This article shares the specific code of JavaScri...

Detailed tutorial on installing mysql 8.0.13 (rpm) on Centos7

yum or rpm? The yum installation method is very c...

How does Vue track data changes?

Table of contents background example Misconceptio...

7 cool dynamic website designs for inspiration

In the field of design, there are different desig...