React 备忘清单
===
适合初学者的综合 React 备忘清单。
入门
----
### 介绍
React 是一个用于构建用户界面的 JavaScript 库。
- [React 官方文档](https://reactjs.org/) _(reactjs.org)_
```js
import React from 'react'
import {createRoot} from 'react-dom/client'
import App from './App'
```
-----
```jsx
const elm = document.getElementById('app')
const root = createRoot(elm);
root.render();
```
### 导入多个导出
```jsx
import React, {Component} from 'react'
import ReactDOM from 'react-dom'
```
-----
```jsx
export class Hello extends Component {
...
}
```
使用 `export` 或者 `export default` 导出 `Hello` 组件
```jsx
import { Hello } from './hello.js';
const Example = ;
```
使用 `import` 导入 `Hello` 组件,在示例中使用。
### React 组件中的 CSS
```jsx
import React from "react";
import "./Student.css";
export const Student = (
);
```
注意:类属性 `className`
```jsx
const divStyle = {
backgroundImage: 'url(' + imgUrl + ')',
};
export const Student = (
);
```
### 属性
```jsx
```
函数组件 `Student` 中访问属性
```jsx
function Student(props) {
return Hello, {props.name}
;
}
```
Class 组件 `Student` 中访问属性
```jsx
class Student extends React.Component {
render() {
return (
Hello, {this.props.name}
);
}
}
```
`class` 组件使用 `this.props` 访问传递给组件的属性。
### Children
```jsx
function Example() {
return (
您有待处理的通知
)
}
```
函数 `AlertBox` 组件
```jsx
function AlertBox(props) {
return (
{props.children}
);
}
```
----
```jsx
{props.children}
```
Class `AlertBox` 组件,与函数组件 `AlertBox` 组件相同
```jsx
class AlertBox extends React.Component {
render () {
return (
{this.props.children}
);
}
}
```
----
```jsx
{this.props.children}
```
`children` 作为子组件的的属性传递。
### State
函数中的 State,Hook 是 React 16.8 的新增特性
```jsx
import { useState } from 'react';
function Student() {
// 声明一个叫 "count" 的 state 变量
const [count, setCount] = useState(0);
const click = () => setCount(count + 1);
return (
);
}
```
使用 `setState` 更新状态,下面是函数组件读取状态
```jsx
您点击了 {count} 次
```
#### Class 中的 State
```jsx
import React from 'react';
class Student extends React.Component {
constructor(props) {
super(props);
this.state = {count: 1};
}
click() {
const count = this.state.count;
this.setState({ count: count + 1})
}
render() {
return (
您点击了{this.state.count}次
);
}
}
```
使用 `setState` 更新状态,`class` 组件中不能使用 ~~hooks~~。下面是 `class` 组件读取状态
```jsx
您点击了{this.state.count}次
```
### 循环
```jsx
const elm = ['one', 'two', 'three'];
function Student() {
return (
{elm.map((value, index) => (
- {value}
))}
);
}
```
`key` 值在兄弟节点之间必须唯一
### 事件监听
```jsx
export default function Hello() {
function handleClick(event) {
event.preventDefault();
alert("Hello World");
}
return (
Say Hi
);
}
```
### 函数注入
```jsx
function addNumbers(x1, x2) {
return x1 + x2;
}
const element = (
{addNumbers(2, 5)}
)
```
### 嵌套
```jsx
import { useState } from 'react'
import Avatar from './Avatar';
import Profile from './Profile';
function Student() {
const [count, setCount] = useState(0);
return (
);
}
```
### Fragment
```jsx
import { Fragment } from 'react'
import Avatar from './Avatar';
import Profile from './Profile';
const Student = () => (
)
```
从 @v16.2.0 开始,`Fragment` 可用于返回多个子节点,而无需向 DOM 添加额外的包装节点。
### Portals
React 并*没有*创建一个新的 `div`。它只是把子元素渲染到 `domNode` 中。`domNode` 是一个可以在任何位置的有效 DOM 节点。
```jsx
render() {
return ReactDOM.createPortal(
this.props.children,
domNode
);
}
```
提供了一种将子节点渲染到存在于父组件以外的 DOM 节点的优秀的方案
默认值
---
### Class 组件默认 props
```jsx
class CustomButton extends React.Component {
// ...
}
CustomButton.defaultProps = {
color: 'blue'
};
```
#### 使用
```jsx
;
```
不传值 `props.color` 将自动设置为 `blue`
### Class 组件默认 state
```jsx
class Hello extends Component {
constructor (props) {
super(props)
this.state = { visible: true }
}
}
```
在构造 `constructor()`中设置默认状态。
```jsx
class Hello extends Component {
state = { visible: true }
}
```
### 函数组件默认 props
```jsx
function CustomButton(props) {
const { color = 'blue' } = props;
return {color}
}
```
### 函数组件默认 state
```jsx
function CustomButton() {
const [color, setColor]=useState('blue')
return {color}
}
```
JSX
---
### 介绍
`JSX` 仅仅只是 `React.createElement(component, props, ...children)` 函数的语法糖
```jsx
点击我
```
会编译为
```js
React.createElement(
MyButton,
{color: 'blue', shadowSize: 2},
'点击我'
);
```
没有子节点
```jsx
```
会编译为
```js
React.createElement(
'div',
{className: 'sidebar'}
)
```
### JSX 点语法
```jsx
const Menu = ({ children }) => (
{children}
);
Menu.Item = ({ children }) => (