網站首頁 編程語言 正文
1. 創建方式
// 寫法一
const Hello = (props) => {
return <div>{props.message}</div>
}
// 寫法二
const Hello = props => <div>{props.message}</div>
// 寫法三
function Hello(props) {
return <div>{props.message}</div>
}
2. 函數組件代替類組件
面臨的問題
- 函數組件沒有state => React v16.8.0推出Hooks API,其中的一個API叫做useState可以解決問題
- 函數組件沒有生命周期 => React v16.8.0推出Hooks API,其中的一個API叫做useEffect可以解決問題
我們對比一下兩種組件實現 n + 1 的例子
類組件
class App extends React.Component {
constructor(props) {
super(props);
this.state = {
n: 0
}
}
addNum = () => {
this.setState({n: this.state.n + 1})
}
render() {
return (
<div className='App'>
<span>n:{this.state.n}</span>
<button onClick={this.addNum}>n+1</button>
</div>
)
}
}
函數組件
const App = props => {
const [n,setN] = React.useState(0)
function addNum(){
setN(n + 1)
}
return (
<div className='App'>
{n}
<button onClick={addNum}>+1</button>
</div>
)
}
相比之下函數組件更為簡潔一些
使用 useEffect 解決生命周期問題
1.模擬 componentDidMount 首次渲染
useEffect(() => { // 模擬componentDidMount 首次渲染
console.log('use effect')
},[]) // 空數組必須寫
2.模擬 componentDidUpdate
const [n, setN] = React.useState(0)
useEffect(() => { // 模擬 componentDidUpdate
console.log('n 變化了')
},[n]) // 數組里面可以寫多個參數表示監聽多個變量
useEffect(() => { // 模擬 componentDidUpdate
console.log('任意屬性變更了')
}) // 不寫數組表示監聽所有 useState 的變量
// 但是這樣在第一次渲染時也會觸發函數的執行
解決方法使用自定義Hook 見下一標題
3.模擬componentWillUnmount
useEffect(() => {
return () => {
console.log('Child 銷毀了')
}
}) // 返回一個函數 在銷毀時執行
4.constructor
函數組件執行的時候,就相當于constructor
5.shouldComponentUpdate
后面的 React.memo和useMemo可以解決
6.render
函數組件的返回值就是render的返回值.
// 模擬render里面寫邏輯
const X = (props) => {
console.log('我是要寫的邏輯')
return (
<div>邏輯模擬</div>
)
}
const App = props => {
let [childVisible, setChildVisible] = useState(true)
const changeVisible = () => {
setChildVisible(!childVisible)
}
return (
<div className='App'>
{childVisible ? <button onClick={changeVisible}>{childVisible}</button> :
<button onClick={changeVisible}>hide</button>}
{/*{childVisible ? <Child/> : null}*/}
<Child/>
<X/>
</div>
)
} // 一個函數便是一個組件
3. 自定義 Hook 之 useUpdate
解決上面 n 值初次渲染就執行的問題
const App = props => {
const [n, setN] = useState(0)
const onClick = () => {
setN(n + 1)
}
const [nUpdateCount, setNUpdateCount] = useState(0)
useEffect(() => { // 初次渲染就執行 + 1
setNUpdateCount(nUpdateCount + 1)
}, [n])
useEffect(() => { // 初次渲染就執行 判斷是否大于1
if(nUpdateCount > 1){
console.log('n變了')
}
},[nUpdateCount])
return (
<div className='App'>
n值變成了:{n}
<button onClick={onClick}>n+1</button>
</div>
)
}
// 通過使用兩次 useEffect 第一個觸發第二個 useEffect 函數計數,大于0便是n值變化了
上面的代碼很亂 改進一下
const useX = (fn, dep) => { // 這就是自定義 Hook 這就可以抽離成別的文件
const [count, setCount] = useState(0)
useEffect(() => {
setCount(x => x + 1)
}, [dep])
useEffect(() => {
if (count > 1) {
fn()
}
}, [count,fn])
}
const App = props => {
const [n, setN] = useState(0)
const onClick = () => {
setN(n + 1)
}
useX(() => {
console.log('n 變化了')
}, n)
return (
<div className='App'>
n值變成了:{n}
<button onClick={onClick}>n+1</button>
</div>
)
}
補充:函數組件代替 class 組件
為什么要用函數組件代替 class 組件?別問,簡單!相比類組件來說,函數組件確實要簡單太多, 不妨看一個 +1 的例子:
class App extends React.Component {
const App = props = > {
constructor() {
const[n, setN] = React.useState(0);
super();
const addN = () = > {
this.state = {
setN(n + 1);
n: 0
}
};
return (
} < div > {
n
}
addN = () = > { < button onClick = {
addN
} > +1 < /button></div >
this.setState({
n: this.state.n + 1
});)
};
}
render() {
return ( < div className = "App" > {
this.state.n
} < button onClick = {
this.addN
} > +1 < /button>
</div > ); //這是公共的渲染部分
}
const rootElement = document.getElementById("root");
}
ReactDOM.render( < App / > , rootElement);
通過上面的例子你可以看出,同樣是實現 +1 的操作,類組件要比函數組件復雜的多,類組件不僅涉及到 extends、setState 等 API,還會涉及到 this 的使用,而且代碼量還很多。反觀函數組件就要清爽的多,所以在開發中推薦使用函數組件。
總結
原文鏈接:https://blog.csdn.net/l_ymttt/article/details/124347990
相關推薦
- 2023-01-03 Kotlin?Thread線程與UI更新詳解_Android
- 2022-07-06 R語言繪制條形圖及分布密度圖代碼總結_python
- 2022-05-22 Nginx安裝后常用功能配置基礎篇_nginx
- 2022-05-10 原生ajax 設置get請求參數和請求頭信息和發送 post請求
- 2022-06-06 ASP.NET的Core?AD域登錄過程示例_ASP.NET
- 2022-07-11 MongoDB分片方式及片鍵選擇
- 2022-06-11 C#實現DataTable轉TXT、CSV文件_C#教程
- 2022-05-18 React?Hook之使用State?Hook的方法_React
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支