context: 實現跨層級的組件數據傳遞
```
import React, { createContext } from 'react';
import logo from './logo.svg';
// 1. 引入 createContext()
const BatteryContext = createContext();
function Leaf() {
return (
// 數據消耗者
<BatteryContext.Consumer>
{
battery => <h1>Battery: {battery}</h1>
}
</BatteryContext.Consumer>
)
}
function Middle() {
return (
<Leaf />
)
}
class App extends React.Component {
state = {
battery: 60
}
render() {
const {battery} = this.state
return (
// 數據提供者
<BatteryContext.Provider value={battery}>
<button type="button" onClick={()=>this.setState({battery:50})}>點擊</button>
<Middle />
</BatteryContext.Provider>
)
}
}
export default App;
```
- 第一章:react 基礎
- 1. 關于 react
- 2. react 工作原理
- 0. 開發環境搭建
- 1. 創建并使用一個組件
- 1. 模塊化與組件化
- 2. 虛擬DOM
- 3. Diff 算法
- React 與 Vue 的使用差異
- 1. 組件創建方式
- 2. data 與 state
- 3. 方法使用方式&this
- 4. 數據雙向綁定
- 5. props
- 6. ref
- 7. for 循環元素
- 8. 生命周期
- create-react-app 改為多頁面應用
- react修改打包路徑,直接查看
- redux
- context
- lazy 實現延遲加載靜態屬性
- Memo實現指定組件進行渲染
- React Hooks
- React Hooks 的概念和意義
- 使用 useState
- axios單次切換formdata和payreload
- react 動態綁定 class
- 高階組件
- react設計模式