react項目中的redux以及react-router-dom

掃盲知識點:

1 傳遞自定義事件:

? ? ? ? <button onClick={(e)=>{change(e)}}>獲取事件對象e</button>

? ? ? ? 將事件對象e傳遞到了change的這個方法中。

2 同時傳遞自定義事件和參數:

???????? <button onClick={(e)=>{change(‘我是參數’,e)}}>獲取事件對象e</button>

? ? ? ? 調用參數和事件對象e; change(str,e)

3 useState遵循狀態不可變的原則,不能修改useState的原始值,只能老值對新值進行替換,也就

? ? ? ? ?是任何對修改源數據的方法都是不可取的,比如數組的增刪截取等,都會修改原數組。

? ? ? ? ?遇到對象和數組的情況下,就可以使用ES6的展開運算符等進行替換。

4? 關于className以及classnames插件的用法 :

? ? ? ? <div className={`active? ${isShow? ' active1' :' '}` }><div> 動態添加類名

? ? ? ? <div className={classNames ( 'active' , { 'active1':true } ) } />

5? 夸層級組件之間的通信方式: Context;

? ? ? ? 執行步驟: 第一步創建實例createContext; 第二步實例的provide提供數據,第三步

????????useContext消費數據:

? ? ? ? 在父組件: import {createContext} from 'react' ;? const ctx = createContext();

? ? ? ? function App(){return ( <ctx.Provider value={count}> <div>app組件</div>

????????</ctx.Provider>)}

? ? ? ? 子組件 import {useContext} from? ' react ' ; import ctx from ‘./app.js’;?

????????const a = useContext(ctx);

6 useEffect的理解:由渲染本身引起的操作,可以發送axios請求,更改DOM等。
????????
? ? useEffect(()=>{}) // 組件渲染完畢會執行一次,組件每次有數據發生改變的時候也會更新

? ? ?useEffect(()=>{},[]) // 該函數只會在渲染完畢之后執行一次,以后將不會再執行;

? ? ?useEffect(()=>{},[count]) / 組件渲染完畢觸發一次,依賴狀態值發生改變觸發

7 redux狀態管理工具:

? ? ? ? 使用步驟: 1 定義一個reducer的函數(根據當前想要做的修改返回一個新的狀態)
? ? ? ? ? ? ? ? ? ? ? ? ? ?2 使用createStore方法傳入reducer函數,生成一個store的實例。
? ? ? ? ? ? ? ? ? ? ? ? ? ?3 使用store實例的subscribe方法訂閱數據的變化(數據發生變化得到通知);
? ? ? ? ? ? ? ? ? ? ? ? ? ?4 使用store實例的dispatch方法提交action對象觸發數據變化
? ? ? ? ? ? ? ? ? ? ? ? ? ?5 使用store的getState的方法獲取最新的數據

? ? ? ? <html>
? ? ? ? ? ? ? ? <button id="increment">+</button>
? ? ? ? ? ? ? ? <script href="加載redux的cdn"></script>

? ? ? ? ? ? ? ? 1 const reducer = (state={count:0}, action){
? ? ? ? ? ? ? ? ? ? ? ? if(action.type==='add'){
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? return {count: state.count++}
????????????????????????}else if(action.type==='dee'){
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? return {count:state.count--}
????????????????????????}else{ return state}
????????????????}

? ? ? ? ? ? ? ? 2 const store = Redux.createStore(reducer)

? ? ? ? ? ? ? ? 3 store.subscrbe(()=>{console.log('發生變化了')})

? ? ? ? ? ? ? ? 4 document.queryselector('button').addEventlister('click',function()
????????????????{store.dispatch(reducer({type:'add'}))})

? ? ? ? ? ? ? ? 5?store.subscrbe(()=>{console.log(store.getState().count)})
????????</html>

8 reduxjs/toolkit(切片)的使用:

? ? ? ? 第一步安裝: npm i react-redux @reduxjs/tookit

? ? ? ? 第二步 src創建store文件夾,store文件夾下創建index.js文件與module的文件夾,

? ? ? ? 第三步 創建count切片:

? ? ? ? 1 引入切片 const {createSlice} from '@reduxjs/toolkit'

? ? ? ? 2 創建reducer的函數并把相關同步異步方法以及reducer導出

? ? ? ? const countSlice = createSlice({

? ? ? ? ? ? ? ? name:'countSlice',
????????????????
? ? ? ? ? ? ? ? initialState:{ countLIst:[] };

? ? ? ? ? ? ? ? reducers:{
? ? ? ? ? ? ? ? ? ? ? ? addCountList(state,action){
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? return state.countList.push(action.paylot)
?????????????????????????}
????????????????}
????????})
? ? ? ? export? function setCountListAsync(){
????????
? ? ? ? ? ? ? ? return async (dispatch)=>{
? ? ? ? ? ? ? ? ? ? ? ? const res = await axios.get('xxxxx')
????????????????????????
? ? ? ? ? ? ? ? ? ? ? ? dispatch(addCountList(res.data))
????????????????}
????????}
? ? ? ? const {addCountList } = countSlice.actions
? ? ? ? export {addCountList}?
? ? ? ? const countStore = countSlice.reducer
? ? ? ? export default countStore

? ? ? ? 3 index.js文件中導入;
? ? ? ??
? ? ? ? import {configureStore} from '@reduxjs/toolkit'

? ? ? ? import countStore from './module/count.js'

? ? ? ? const store = configureStore({
? ? ? ? ? ? ? ? reducers:{
? ? ? ? ? ? ? ? ? ? ? ? count:countStore,
????????????????}
????????})

? ? ? ? export default store

? ? ? ? 4 index.js入口文件中全局注冊:

? ? ? ? import {Provider} from 'react-redux';

? ? ? ? import store from './store/index.js'

? ? ? ? <Provider store={store}><App></App></Provider>

? ? ? ? 5 組件中使用:import {useSelector,useDispatch} from 'react-redux'
????????????????
? ? ? ? ? ? ? ? const? dispatch = useDIspatch()? ? ? ? ? ? ? ??

? ? ? ? ? ? ? ?獲取數據 useSelector(state=>state.count.countList)

? ? ? ? ? ? ? ?調用異步的方法 dispathch(setCountListAsync)

9 react-router-dom的使用:
????????
? ? ? ? 1 進行依賴的安裝: npm install react-router-dom

? ? ? ? 2 創建router文件夾,router文件下創建index.js文件配置路由:

? ? ? ? import {createBrowseRouter} from 'react-router-dom'

? ? ? ? const routes = createBrowserRouter([
? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? ? ? path:'/home',

? ? ? ? ? ? ? ? ? ? ? ? element:<home></home>,

? ? ? ? ? ? ? ? ? ? ? ? children:[
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?index:true, // 進入home路由 默認展示 下面這個路由組件

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? element:<Children><Children>
????????????????????????????????},

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? path:'son',

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ellement:<Son></Son>

????????????????????????????????}
????????????????????????]
????????????????}
????????])

? ? ? ? 3 項目的index.js文件中,用來包裹APP組件:

? ? ? ? import {RouterProvider} from 'react-router-dom'

? ? ? ? import {routes} from './router/index.js'

? ? ? ? <RouterProvider router={routes}></ RouterProvider>

? ? ? ? 4 如果是在二級出口的話? 需要使用<Outlet></Outlet>? ?<Link to="/son"></Link>? 這里不需要
????????加父組件的路徑了。

? ? ? ? 5 searchParams 傳參 與 params傳參。

? ? ? ? ? ?<Link to="/home?id=10&name='zs' "></Link>? 傳遞參數

? ? ? ? ? ?const [params] = useSearchParams()? console.log(params.get('id')) // 10?

? ? ? ? ? ? <Link to='./home/12'>params傳參</Link>??

? ? ? ? ? ? ?const params =? useParams()? let id = params.id // 12

????????

? ? ? ? ?

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/697069.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/697069.shtml
英文地址,請注明出處:http://en.pswp.cn/news/697069.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

基于微信小程序失物招領系統設計與實現(PHP后臺+Mysql)可行性分析

博主介紹&#xff1a;黃菊華老師《Vue.js入門與商城開發實戰》《微信小程序商城開發》圖書作者&#xff0c;CSDN博客專家&#xff0c;在線教育專家&#xff0c;CSDN鉆石講師&#xff1b;專注大學生畢業設計教育和輔導。 所有項目都配有從入門到精通的基礎知識視頻課程&#xff…

CleanMyMac2024蘋果電腦清理工具最新使用全面評價

作為軟件評價專家&#xff0c;我對CleanMyMac X進行了全面的評估&#xff0c;以下是我的詳細評價&#xff1a; CleanMyMac X4.14.6全新版下載如下: https://wm.makeding.com/iclk/?zoneid49983 一、功能 CleanMyMac X的功能相當全面&#xff0c;幾乎涵蓋了Mac電腦清理所需的…

nginx 具體介紹

一&#xff0c;nginx 介紹 &#xff08;一&#xff09;nginx 與apache 1&#xff0c; Apache event 模型 相對于 prefork 模式 可以同時處理更多的請求 相對于 worker 模式 解決了keepalive場景下&#xff0c;長期被占用的線程的資源浪費問題 因為有監聽線程&#…

【數據結構】鏈式隊列

鏈式隊列實現&#xff1a; 1.創建一個空隊列 2.尾插法入隊 3.頭刪法出隊 4.遍歷隊列 一、main函數 #include <stdio.h> #include "./3.linkqueue.h" int main(int…

文檔控件DevExpress Office File API v23.2新版亮點 - 支持SVG

DevExpress Office File API是一個專為C#, VB.NET 和 ASP.NET等開發人員提供的非可視化.NET庫。有了這個庫&#xff0c;不用安裝Microsoft Office&#xff0c;就可以完全自動處理Excel、Word等文檔。開發人員使用一個非常易于操作的API就可以生成XLS, XLSx, DOC, DOCx, RTF, CS…

數據結構之單鏈表的操作

main函數 #include <stdio.h> #include "./03_linkList.h" int main(int argc, const char *argv[]) { linkList* head creatr_linkList(); insertHead_linkL…

運維SRE-19 網站Web中間件服務-http-nginx

Ans自動化流程 1.網站集群核心協議&#xff1a;HTTP 1.1概述 web服務&#xff1a;網站服務&#xff0c;網站協議即可. 協議&#xff1a;http協議,https協議 服務&#xff1a;Nginx服務&#xff0c;Tengine服務....1.2 HTTP協議 http超文本傳輸協議&#xff0c;負責數據在網站…

更高效的構建工具-vite

更高效的構建工具-vite 前言Vite是什么Vite和webpack的比較1. 運行原理2. 使用成本 Vite的初體驗 前言 首先我們要認識什么時構建工具&#xff1f; 企業級項目都具備什么功能呢&#xff1f; Typescript&#xff1a;如果遇到ts文件&#xff0c;我們需要使用tsc將typescript代碼…

Android約束布局中用ConstraintHelper實現過渡動畫效果

前些天發現了一個蠻有意思的人工智能學習網站,8個字形容一下"通俗易懂&#xff0c;風趣幽默"&#xff0c;感覺非常有意思,忍不住分享一下給大家。 &#x1f449;點擊跳轉到教程 一.創建一個類CircularRevealHelper繼承ConstraintHelper代碼如下 /*** Author: ly* Da…

【Linux從青銅到王者】 基礎IO

本篇重點&#xff1a;文件描述符&#xff0c;重定向&#xff0c;緩沖區&#xff0c;磁盤結構&#xff0c;文件系統&#xff0c;inode理解文件的增刪查改&#xff0c;查找一個文件為什么一定要有路徑&#xff0c;動靜態庫&#xff0c;有的時候為什么找不到庫&#xff0c;動態庫的…

JavaWeb——003Axios Vue組件庫(Element)

目錄 一、Ajax 1、同步與異步?編輯 2、原生Ajax&#xff08;繁瑣&#xff09;?編輯 2.1、寫一個簡易的Ajax 3、Axios&#xff08;推薦使用&#xff09;?編輯 3.1、Axios入門 3.2、Axios請求方式別名 3.3、案例&#xff1a;基于Vue及Axios完成數據的動態加載展示?編…

Flink CDC 3.0 表結構變更時導致webUI接口無反應原因

Flink CDC 3.0 表結構變更時導致webUI接口無反應&#xff01; 原因&#xff1a;因為deliverCoordinationRequestToCoordinator和requestJob都是SchedulerNG中方法&#xff0c;該類的線程模型是單線程執行&#xff0c;所以在deliverCoordinationRequestToCoordinator執行表結構…

mysql創建數據庫,用戶授權

一、創建用戶 CREATE USER 用戶名% IDENTIFIED BY 密碼; flush privileges; 二、更新用戶密碼 update mysql.user set authentication_stringpassword("密碼") where userroot; flush privileges; 三、允許root遠程登錄 update user set host % where user r…

AIoT網關 人工智能物聯網網關

AIoT(人工智能物聯網)作為新一代技術的代表&#xff0c;正以前所未有的速度改變著我們的生活方式。在這個智能時代&#xff0c;AIoT網關的重要性日益凸顯。它不僅是連接智能設備和應用的關鍵&#xff0c;同時也是實現智能化家居、智慧城市和工業自動化的必備技術。      一…

c# entity freamwork 判斷是否存在

在 Entity Framework (EF) 中&#xff0c;你可以使用 LINQ 查詢來判斷數據庫中是否存在特定條件的記錄。以下是一些常見的方法&#xff1a; 使用 Any 方法: using (var context new YourDbContext()) {bool exists context.YourEntity.Any(e > e.Property yourValue);i…

【linux進程間通信(二)】共享內存詳解以及進程互斥概念

&#x1f493;博主CSDN主頁:杭電碼農-NEO&#x1f493; ? ?專欄分類:Linux從入門到精通? ? &#x1f69a;代碼倉庫:NEO的學習日記&#x1f69a; ? &#x1f339;關注我&#x1faf5;帶你學更多操作系統知識 ? &#x1f51d;&#x1f51d; 進程間通信 1. 前言2. 共享內…

2024年2月23日 晨會匯報

Good morning, colleages! This is /?dɑ?.t?i/ speaking. As for my report today, I decide to wing it, so I didnt prepare a script. Now, Ill share an update about my recent work activities which encompasses two key area: a summary of my work yesterday a…

【Go channel如何控制goroutine并發執行順序?】

多個goroutine并發執行時&#xff0c;每一個goroutine搶到處理器的時間點不一致&#xff0c;gorouine的執行本身不能保證順序。即代碼中先寫的gorouine并不能保證先執行 思路&#xff1a;使用channel進行通信通知&#xff0c;用channel去傳遞信息&#xff0c;從而控制并發執行…

基于開源模型對文本和音頻進行情感分析

應用場景 從商品詳情頁爬取商品評論&#xff0c;對其做輿情分析&#xff1b;電話客服&#xff0c;對音頻進行分析&#xff0c;做輿情分析&#xff1b; 通過開發相應的服務接口&#xff0c;進一步工程化&#xff1b; 模型選用 文本&#xff0c;選用了通義實驗室fine-tune的st…

電腦錄屏軟件哪個好用?實測告訴你答案(2024年最新)

在當今信息化快速發展的時代&#xff0c;無論是錄制在線課程、游戲操作&#xff0c;還是制作教程、會議記錄&#xff0c;一款電腦錄屏軟件顯得尤為重要&#xff0c;可是電腦錄屏軟件哪個好用呢&#xff1f;本文將介紹三款主流的電腦錄屏軟件&#xff0c;通過分步驟詳細講述&…