最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

react中函數(shù)式組件React Hooks詳解

 更新時(shí)間:2025年05月06日 14:23:33   作者:聒噪,  
這篇文章主要介紹了react中函數(shù)式組件React Hooks的使用方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教

React Hooks

函數(shù)式組件

使用hooks理由

  1. 高階組件為了復(fù)用,導(dǎo)致代碼層級(jí)復(fù)雜
  2. 生命周期的復(fù)雜
  3. 寫(xiě)成functional組件,無(wú)狀態(tài)組件,因?yàn)樾枰獱顟B(tài),又改成了class,成本高

在16.8版本之前函數(shù)式組件無(wú)法寫(xiě)狀態(tài)所以一般只能用class組件成本比較高代碼嵌套比較深,并且鉤子函數(shù)(生命周期相對(duì)來(lái)說(shuō)過(guò)于復(fù)雜)在16.8之后出了函數(shù)式組件的狀態(tài)后,很好的解決了這些弊端。

useState

在之前版本中函數(shù)式組件是不支持狀態(tài)的為了讓函數(shù)式組件更好的支持狀態(tài),可以利用useState

const [name,setName] = useState("Riven")

主要利用這個(gè)useState的方法 也是一個(gè)數(shù)組它有個(gè)值一個(gè)是Riven,另一個(gè)是修改這個(gè)狀態(tài)的方法f()你可以打印一下自己查看console.log(useState(“Riven”)) 如上代碼就是將這個(gè)狀態(tài)和這個(gè)修改狀態(tài)的方法給解構(gòu)出來(lái)這樣就可以訪(fǎng)問(wèn)到狀態(tài),并且進(jìn)行修改了

// rcc => react component class
// rfc => react component function

import React,{useState} from 'react'
//利用useState讓函數(shù)式組件支持狀態(tài)

function App(){
    console.log(useState("Riven"));
//不支持狀態(tài)
    const [name,setName] = useState("Riven")
    const [age,setAge] = useState(25)
    return(
        <div>
            app----{name}---{age}
            <button onClick={()=>{
                setName("kerwin")
                setAge(100)
            }}>click</button>
        </div>
    )
}

export default App

以上就是函數(shù)式組件的基本寫(xiě)法了你肯定有許多問(wèn)題?函數(shù)式組件的生命周期呢?函數(shù)式組件的傳值呢通信呢?沒(méi)有類(lèi)組件的寫(xiě)法這些應(yīng)該怎么寫(xiě)呢?

請(qǐng)隨我繼續(xù)往后看

todolist小案例(函數(shù)式組件)

首先怎么獲取input框中的value值呢?

這里我們有兩種方法和類(lèi)組件差不多

  1. 就是利用onChage事件
import React,{useState} from 'react'

function App(){
    const [text, setText] = useState("")
    return(
        <div>
            <input type="text" onChange={(ev)=>{
                // console.log(ev.target)
                setText(ev.target.value)
            }} />

        </div>
    )
}

export default App
  1. 利用ref

這種方法較為復(fù)雜需要利用useRef

import React,{useState,useRef} from 'react'

const mytext = useRef(null)

       <input type="text" onChange={(ev)=>{
                // console.log(ev.target)
                setText(ev.target.value)
            }} ref={mytext} />

訪(fǎng)問(wèn)的時(shí)候可以直接通過(guò)mytext.current.value得到value值

import React,{useState,useRef} from 'react'

function App(){
    const [text, setText] = useState("")
    const mytext = useRef(null)
    return(
        <div>
            <input type="text" onChange={(ev)=>{
                // console.log(ev.target)
                setText(ev.target.value)
            }} ref={mytext} />
            <button onClick={()=>{
                console.log(mytext.current.value)
            }}>add</button>

        </div>
    )
}

export default App

完整的todolist

需要注意學(xué)習(xí)的地方

  • input框value值獲取
  • 怎么讓進(jìn)行添加
  • 怎么讓添加完成后輸入框的value值清空
  • 怎么進(jìn)行刪除
import React,{useState,useRef} from 'react'

function App(){
    const [text, setText] = useState("")
    const [list,setList] = useState(["111","222","333"])
    const mytext = useRef(null)


    //刪除函數(shù)只是想寫(xiě)在外面
  const  handledelclick = (index)=>{
        var newlist = [...list]
        newlist.splice(index,1)
        setList(newlist)
    }


    return(
        <div>
            <input type="text" onChange={(ev)=>{
                // console.log(ev.target)
                setText(ev.target.value)
            }} ref={mytext}  value={text} />
            <button onClick={()=>{
                // console.log(mytext.current.value)  //ref
                setList([...list,text])   //添加數(shù)組
                setText(" ")

            }}>add</button>
            {
                list.map((item,index)=>
                    <li key={item}>
                        {item}
                        <button onClick={()=>handledelclick(index)}>del</button>
                    </li>
                )
            }
        </div>
    )
}

export default App

useEffect

在函數(shù)式組件中我們沒(méi)有生命周期useEffect類(lèi)似生命周期但不是生命周期

我們把數(shù)據(jù)請(qǐng)求放在函數(shù)組件的哪里呢?

  • 首先需要知道useEffect接收兩個(gè)參數(shù),第一個(gè)是處理函數(shù),第二是依賴(lài)
  • 第一個(gè)參數(shù)相當(dāng)于一旦創(chuàng)建就會(huì)執(zhí)行。
  • 如果第二個(gè)參數(shù)傳入的依賴(lài)狀態(tài)改變也會(huì)自動(dòng)執(zhí)行
  • 如果第二個(gè)參數(shù)傳入的依賴(lài)狀態(tài)為空就相當(dāng)于值會(huì)走一次
  • 如果不傳任何狀態(tài)改變都會(huì)觸發(fā)第一個(gè)處理函數(shù)的執(zhí)行
  • return 出來(lái)的這個(gè)函數(shù)只有銷(xiāo)毀的時(shí)候才會(huì)執(zhí)行
    useEffect(()=>{
        console.log("創(chuàng)建");
        var id = setInterval(()=>{
            console.log(111);
        },2000)
        return ()=>{
            console.log("銷(xiāo)毀")
            clearInterval(id)
        }
    },[])

下來(lái)看一個(gè)簡(jiǎn)單的案例

import React,{useState,useEffect} from 'react'

const Child = ()=>{
    useEffect(()=>{
        console.log("創(chuàng)建");
        var id = setInterval(()=>{
            console.log(111);
        },2000)
        return ()=>{
            console.log("銷(xiāo)毀")
            clearInterval(id)
        }
    },[])
    return(
        <div>
            Child
        </div>
    )
}

const App = ()=>{
    //useEffect(處理函數(shù),[依賴(lài)])
    const [text, setText] = useState("1111")
    const [age, setAge] = useState(25)
    // useEffect(()=>{
    //     console.log("創(chuàng)建或者更新的時(shí)候執(zhí)行")
    // },[text])

    //傳入空數(shù)組相當(dāng)于cdm
    //[text] 只有text的改變才會(huì)執(zhí)行一次
    //第二參數(shù)不傳表示任何狀態(tài)改變都會(huì)重新執(zhí)行

    return(
        <div>
            App ----{text} 
            <button onClick={()=>{
                setText("22222")
            }}>click-text</button>
            <br/>
            {age}
            <button onClick={()=>{
                setAge(100)
            }}>click-age</button>
            {
                age===25?
                <Child/>:
                null
            }
        </div>
    )
}

export default App

在實(shí)際應(yīng)用中函數(shù)式組件會(huì)自動(dòng)傳一個(gè)參數(shù)props 這樣我們就可以訪(fǎng)問(wèn)到props里面的屬性了

props.match.params.myid
props.history.goBack()
import React,{useState,useEffect} from 'react'
import { PageHeader} from 'antd';
import axios from 'axios'

const Preview = (props)=>{
    const [title, settitle] = useState("")
    const [content, setcontent] = useState("")
    const [category, setcategory] = useState([])

    useEffect(()=>{
        axios.get(`http://localhost:8000/articles/${props.match.params.myid}`).then(res=>{
            console.log(res.data)
            let {title,content,category} = res.data
            settitle(title)
            setcontent(content)
            setcategory(category)
        })

    },[props])




    return (
        <div>
            <PageHeader
            className="site-page-header"
            onBack={() => {
                // console.log("back",this.props.history)
                props.history.goBack()//返回上一個(gè)頁(yè)面
            }} //返回按鈕
            title={title}
            subTitle={category.join("/")}
        />
            <div style={{ padding: "24px" }} dangerouslySetInnerHTML={{
                __html:content
            }}></div>
        </div>
    )
}


export default Preview

useCallback

防止因?yàn)榻M件重新渲染,導(dǎo)致方法被重新創(chuàng)建 ,起到緩存作用; 只有第二個(gè)參數(shù) 變化了,才重新聲明一次

也就是只需要當(dāng)需要某個(gè)狀態(tài)改變的時(shí)候才讓函數(shù)執(zhí)行一次

import React,{useState,useCallback} from 'react'

export default function App() {
    const [text,settext] = useState("1111")
    const test = useCallback(
        () => {
            console.log(text)
        },
        [text],
    )
    test()
    return (
        <div>
            App-{text}
            <button onClick={()=>{
                settext("22222")
            }}>click</button>
        </div>
    )
}

useContext useReducer

首先這兩個(gè)都是用來(lái)在函數(shù)式組件中進(jìn)行非父子通信的

首先需要?jiǎng)?chuàng)建一個(gè)GlobalContext 用來(lái)包裹這些生產(chǎn)者的

import React from 'react'
const GlobalContext = React.createContext()

export default GlobalContext

然后引入

import GlobalContext from './store'

總體代碼如下

import React,{useReducer,useContext} from 'react'
import GlobalContext from './store'
import reducer from './store/reducer'
import axios from 'axios'

const Child1 = ()=>{
    // useContext(GlobalContext)
    // console.log(useContext(GlobalContext))
    let {state,dispatch} = useContext(GlobalContext)
   
    return (
        <div>
            child1-----{state.text}
            <button onClick={()=>{
                dispatch({
                    type:"Change_text",
                    payload:"child11111111111"
                })
            }}>click</button>
        </div>
    )
}

const Child2 = ()=>{
    let {state,dispatch} = useContext(GlobalContext)
    return (
        <div>
            Child2----{state.text}
            <button onClick={()=>{
                axios.get("http://localhost:8000/rights").then(res=>{
                    // console.log(res.data)
                    dispatch({
                        type:"Change_list",
                        payload:res.data
                    })
                })
            }}>異步數(shù)據(jù)</button>
            {
                state.list.map(item=>
                    <li key={item.id}>{item.title}</li> 
                )
            }
        </div>
    )
}


const App = ()=>{
    const [state,dispatch] = useReducer(reducer,{
        isShow:true,
        list:[],
        text:"我是公共的狀態(tài)"
    })  //[公共的狀態(tài),改變公共狀態(tài)的方法]

    return (
        <GlobalContext.Provider value={{
                state,
                dispatch
            }}>
            <Child1/>
            <Child2/>
        </GlobalContext.Provider>
    )
}

export default App

這里需要注意

   const [state,dispatch] = useReducer(reducer,{
        isShow:true,
        list:[],
        text:"我是公共的狀態(tài)"
    })  //[公共的狀態(tài),改變公共狀態(tài)的方法]

這塊useReducer(reducer) 其實(shí)相當(dāng)于一個(gè)數(shù)組里面有兩個(gè)值一個(gè)是公共的狀態(tài)另外一個(gè)改變公共狀態(tài)的方法

訪(fǎng)問(wèn)的時(shí)候可以通過(guò)usecontext(GlobalContext) 里面有兩個(gè)值一個(gè)為state,一個(gè)為dispatch

const Child1 = ()=>{
    // useContext(GlobalContext)
    // console.log(useContext(GlobalContext))
    let {state,dispatch} = useContext(GlobalContext)
   
    return (
        <div>
            child1-----{state.text}
            <button onClick={()=>{
                dispatch({
                    type:"Change_text",
                    payload:"child11111111111"
                })
            }}>click</button>
        </div>
    )
}

異步也差不多

const Child2 = ()=>{
    let {state,dispatch} = useContext(GlobalContext)
    return (
        <div>
            Child2----{state.text}
            <button onClick={()=>{
                axios.get("http://localhost:8000/rights").then(res=>{
                    // console.log(res.data)
                    dispatch({
                        type:"Change_list",
                        payload:res.data
                    })
                })
            }}>異步數(shù)據(jù)</button>
            {
                state.list.map(item=>
                    <li key={item.id}>{item.title}</li> 
                )
            }
        </div>
    )
}

reducer文件

const reducer = (prevstate,action)=>{
    let {type,payload} = action
    switch(type){
        case "Change_text":
            //深復(fù)制老狀態(tài),返回新?tīng)顟B(tài)
            // prevstate.text
            // var newstate = {...prevstate}

            return {
                ...prevstate,
                text:payload
            } // immutable

        case "Change_list":
            return {
                ...prevstate,
                list:payload
            }
            default:
            return prevstate
    }
   
    
}

export default reducer

函數(shù)式組件的父子通信問(wèn)題

這個(gè)和類(lèi)組件差不多,只不過(guò)是函數(shù)式組件會(huì)自動(dòng)傳一個(gè)參數(shù)props這樣就可以直接訪(fǎng)問(wèn)到props里面的屬性了

  • 父?jìng)髯樱▽傩裕?/li>
  • 子傳父(回調(diào)函數(shù))
// 父子通信 ,靠屬性
// 父->子 , 屬性
// 子->父 , callback( 屬性傳過(guò)去一個(gè)回調(diào)函數(shù))

import React,{useState} from 'react'

const Navbar = (props)=>{
    console.log(props)
    return <div>
        navbar-{props.myname}
        <button onClick={()=>{
            props.onEvent()
        }}>click</button>
    </div>
}

const Sidebar = ()=>{
    return <div>
        Sidebar
        <ul>
            <li>111111</li>
            <li>222222</li>
            <li>333333</li>
        </ul>
    </div>
}

const Child = (props)=>{
    return <div>
        child --{props.children}
    </div>
}

export default function App() {
    const [show, setshow] = useState(false)
    return (
        <div>
            <Navbar myname="測(cè)試的抽屜" onEvent={()=>{
                console.log("父組件中調(diào)用")
                setshow(!show)
            }}/>
            {
                show?
                <Sidebar/>
                :null
            }

            <Child>
                <div>child-11111111</div>
                <div>child-22222222</div>
            </Child>
        </div>
    )
}

總結(jié)

以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。

相關(guān)文章

  • react中將html字符串渲染到頁(yè)面的實(shí)現(xiàn)方式

    react中將html字符串渲染到頁(yè)面的實(shí)現(xiàn)方式

    這篇文章主要介紹了react中將html字符串渲染到頁(yè)面的實(shí)現(xiàn)方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-12-12
  • React中useRef hook的簡(jiǎn)單用法

    React中useRef hook的簡(jiǎn)單用法

    useRef是react的自定義hook,它用來(lái)引用一個(gè)不需要渲染的值,這篇文章介紹useRef的簡(jiǎn)單用法,感興趣的朋友一起看看吧
    2024-01-01
  • React之echarts-for-react源碼解讀

    React之echarts-for-react源碼解讀

    這篇文章主要介紹了React之echarts-for-react源碼解讀,echarts-for-react的源碼非常精簡(jiǎn),本文將針對(duì)主要邏輯分析介紹,需要的朋友可以參考下
    2022-10-10
  • React Scheduler 最小堆實(shí)現(xiàn)小結(jié)

    React Scheduler 最小堆實(shí)現(xiàn)小結(jié)

    本文主要介紹了React Scheduler 最小堆實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2026-01-01
  • react中setState的執(zhí)行機(jī)制詳解

    react中setState的執(zhí)行機(jī)制詳解

    setState() 的執(zhí)行機(jī)制包括狀態(tài)合并、批量更新、異步更新、虛擬 DOM 比較和渲染組件等步驟,這樣可以提高性能并優(yōu)化渲染過(guò)程,這篇文章主要介紹了react中的setState的執(zhí)行機(jī)制,需要的朋友可以參考下
    2023-10-10
  • React?hook實(shí)現(xiàn)簡(jiǎn)單的websocket封裝方式

    React?hook實(shí)現(xiàn)簡(jiǎn)單的websocket封裝方式

    這篇文章主要介紹了React?hook實(shí)現(xiàn)簡(jiǎn)單的websocket封裝方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-09-09
  • React中關(guān)于render()的用法及說(shuō)明

    React中關(guān)于render()的用法及說(shuō)明

    這篇文章主要介紹了React中關(guān)于render()的用法及說(shuō)明,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-02-02
  • react?事項(xiàng)懶加載的三種方法及使用場(chǎng)景

    react?事項(xiàng)懶加載的三種方法及使用場(chǎng)景

    這篇文章主要介紹了react?事項(xiàng)懶加載的三種方法及使用場(chǎng)景,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2023-07-07
  • react源碼中的生命周期和事件系統(tǒng)實(shí)例解析

    react源碼中的生命周期和事件系統(tǒng)實(shí)例解析

    這篇文章主要為大家介紹了react源碼中的生命周期和事件系統(tǒng)實(shí)例解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-01-01
  • react中antd Upload手動(dòng)上傳的示例

    react中antd Upload手動(dòng)上傳的示例

    本文主要介紹了react中antd Upload手動(dòng)上傳的示例,文中通過(guò)示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2022-04-04

最新評(píng)論

淮南市| 长兴县| 体育| 淳安县| 右玉县| 张掖市| 乌什县| 曲麻莱县| 东乌珠穆沁旗| 即墨市| 合肥市| 鹤庆县| 惠东县| 尼勒克县| 长武县| 库伦旗| 大冶市| 广汉市| 昭苏县| 绍兴市| 青冈县| 乐陵市| 九江市| 彭州市| 鹤山市| 丰顺县| 永安市| 安仁县| 内乡县| 海门市| 浏阳市| 西丰县| 旅游| 娄底市| 华阴市| 汝州市| 繁峙县| 石嘴山市| 炎陵县| 油尖旺区| 汝城县|