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

Example of converting JavaScript flat array to tree structure

Table of contents 10,000 pieces of data were lost...

Record the steps of using mqtt server to realize instant communication in vue

MQTT Protocol MQTT (Message Queuing Telemetry Tra...

Basic syntax of MySQL index

An index is a sorted data structure! The fields t...

CSS3 transition to implement notification message carousel

Vue version, copy it to the file and use it <t...

Analysis of the principle and usage of MySQL custom functions

This article uses examples to illustrate the prin...

Detailed explanation of the principle of Vue monitoring data

Table of contents 1. Introduction II. Monitoring ...

Example code for implementing page floating box based on JS

When the scroll bar is pulled down, the floating ...

Detailed explanation of Docker Swarm concepts and usage

Docker Swarm is a container cluster management se...

Install CentOS 7 on VMware14 Graphic Tutorial

Introduction to CentOS CentOS is an enterprise-cl...

How to define input type=file style

Why beautify the file control? Just imagine that a...

Analysis and description of network configuration files under Ubuntu system

I encountered a strange network problem today. I ...