Hello, World!
;}\n\nBreak down a complex UI into a component tree:\n\nAppβββ Headerβ βββ Logoβ βββ Navigationβ βββ NavItemβ βββ NavItemβββ Mainβ βββ Sidebarβ βββ Contentβ βββ Articleβ βββ Articleβββ Footer\n\n* * *\n\n## Function Components\n\nFunction components are a concise way to define components.\n\nA function component is a JavaScript function that accepts props and returns a React element.\n\nBasic syntax:\n\n// Method 1: Function declaration function Welcome(props) { returnHello, {props.name}
;}// Method 2: Arrow function const Welcome = (props) => { returnHello, {props.name}
;};// Method 3: Simplified notation (single-line return) const Welcome = (props) =>Hello, {props.name}
;\n\nCreate a simple function component:\n\n## src/Welcome.js file:\n\nimport React from 'react';\n\n// Define a function component\n\nfunction Welcome(props){\n\nreturnHello,{props.name}!
;\n\n}\n\nexport default Welcome;\n\nRender this component in src/index.js:\n\n## src/index.js file:\n\nimport React from 'react';\n\nimport ReactDOM from 'react-dom';\n\nimport'./index.css';\n\nimport Welcome from './Welcome';\n\nconst root = ReactDOM.createRoot(document.getElementById("root"));\n\n// Render the Welcome component and pass the name attribute\n\n root.render();\n\nThis example shows a simple component that accepts a name attribute and displays "Hello, World!" on the page.\n\n* * *\n\n## Class Components\n\nClass components are defined using ES6 class syntax, typically used when there is a need to manage state or use lifecycle methods.\n\nBasic syntax:\n\nimport React, { Component } from 'react';class Welcome extends Component { render() { returnHello, {this.props.name}
; }}\n\nCreate a class component:\n\n## Example\n\nimport React,{ Component } from 'react';\n\nclass Welcome extends Component {\n\n render(){\n\nreturnHello,{this.props.name}!
;\n\n}\n\n}\n\nexport default Welcome;\n\nRender this component in src/index.js:\n\n## Example\n\nimport React from 'react';\n\nimport ReactDOM from 'react-dom';\n\nimport'./index.css';\n\nimport Welcome from './Welcome';\n\nconst root = ReactDOM.createRoot(document.getElementById("root"));\n\n// Render the Welcome component and pass the name attribute\n\n root.render();\n\n* * *\n\n## Test Example\n\nNext, we will encapsulate a component that outputs "Hello World!", and the component name is HelloMessage:\n\n## React Example\n\nfunction HelloMessage(props){returnHello World!
; }const element = ; const root = ReactDOM.createRoot(document.getElementById("root")); root.render(element);\n\n[Try it out Β»](#)\n\n### Example Analysis:\n\n1. We can use a function to define a component:\n\nfunction HelloMessage(props) { returnHello World!
;}\n\nYou can also use an ES6 class to define a component:\n\nclass Welcome extends React.Component { render() { returnHello World!
; }}\n\n2. **const element = ** is a user-defined component.\n\n> Note that native HTML element names start with a lowercase letter, while custom React class names start with an uppercase letter. For example, HelloMessage cannot be written as helloMessage. In addition, note that a component class can only contain one top-level tag, otherwise it will throw an error.\n\nIf we need to pass parameters to a component, we can use the **this.props** object, as shown in the following example:\n\n## React Example\n\nfunction HelloMessage(props){returnHello{props.name}!
; }const element = ; const root = ReactDOM.createRoot(document.getElementById("root")); root.render(element);\n\n[Try it out Β»](#)\n\nIn the above example, the **name** attribute is retrieved via props.name.\n\n> Note that when adding attributes, the class attribute needs to be written as className, and the for attribute needs to be written as htmlFor. This is because class and for are JavaScript reserved words.\n\n* * *\n\n## Composite Components\n\nWe can create multiple components to synthesize a component, that is, to separate the different functional points of a component.\n\nIn the following example, we implement a component that outputs the website name and URL:\n\n## React Example\n\nfunction Name(props){returnWebsite name:{props.name}
; }function Url(props){return{props.url}
; }function Nickname(props){returnWebsite nickname:{props.nickname}
; }function App(){return(); }const root = ReactDOM.createRoot(document.getElementById("root")); root.render();\n\n[Try it out Β»](#)\n\nIn the example, the App component uses the Name, Url, and Nickname components to output the corresponding information.\n\n* * *\n\n## Props (Properties)\n\nProps are the way to pass data between components, similar to function parameters.\n\n### Basic Usage\n\n// Parent component passes props to function App() { return (Hello, {props.name}!
Age: {props.age}
Hello, {name}!
Age: {age}
This is content
} /> );}\n\n### Children Props\n\nSpecial props used to pass child content to a component:\n\n// Method 1: Using props.childrenfunction Card(props) { return ( {props.children}
{title &&
);}// Using function App() { return ( {title}
}{children}
This is the card content
);}\n\n### Immutability of Props\n\n**Important Principle**: Never modify props!\n\n// Error: Modifying props for function BadComponent(props) { props.name = "Changed"; // Absolutely do not do this! return{props.name}
;}// Correct: Treat props as read-only for function GoodComponent({ name }) { const displayName = name.toUpperCase(); // Create a new value return{displayName}
;}\n\n* * *\n\n## Component Composition and Reuse\n\n### Composition Pattern\n\n// Basic component function Avatar({ src, alt }) { return{name}
{email}
<Avatar src={user.avatar} alt={
YouTip