1. Create components using functionsFunctional 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 classesComponent 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 file1. 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:
|
<<: Introduction to Semantic XHTML Tags
>>: Summary of Css methods for clearing floats
Table of contents infer Case: Deepen your underst...
To summarize the form submission method: 1. Use t...
First, we will introduce how (1) MySQL 5.7 has a ...
The current requirement is: there is a file uploa...
Long story short, today we will talk about using ...
This article mainly introduces the implementation...
Copy code The code is as follows: <!DOCTYPE ht...
When we work in a terminal or console, we may not...
DIV+css structure Are you learning CSS layout? Sti...
Table of contents 1. Embed CSS styles directly in...
1.command not found command not found 2. No such ...
This article shares the specific code of JavaScri...
yum or rpm? The yum installation method is very c...
Table of contents background example Misconceptio...
In the field of design, there are different desig...