中文亚洲精品无码_熟女乱子伦免费_人人超碰人人爱国产_亚洲熟妇女综合网

當(dāng)前位置: 首頁 > news >正文

鹽城市亭湖區(qū)建設(shè)局網(wǎng)站/博客可以做seo嗎

鹽城市亭湖區(qū)建設(shè)局網(wǎng)站,博客可以做seo嗎,南通通州建設(shè)工程質(zhì)量監(jiān)督網(wǎng)站,怎樣建立自己網(wǎng)站多少錢使用 useContext useReducer 實現(xiàn)的輕量級狀態(tài)管理,適合中小型 React 應(yīng)用使用。 🧠 實現(xiàn)思路 使用 createContext 創(chuàng)建兩個上下文:StateContext 和 DispatchContext。用 useReducer 管理狀態(tài)邏輯。創(chuàng)建一個 Provider 組件包裹應(yīng)用。提供兩…

使用 useContext + useReducer 實現(xiàn)的輕量級狀態(tài)管理,適合中小型 React 應(yīng)用使用。


🧠 實現(xiàn)思路

  1. 使用 createContext 創(chuàng)建兩個上下文:StateContextDispatchContext
  2. useReducer 管理狀態(tài)邏輯。
  3. 創(chuàng)建一個 Provider 組件包裹應(yīng)用。
  4. 提供兩個 hooks:useGlobalState()useGlobalDispatch(),分別獲取狀態(tài)和派發(fā)方法。

? 示例代碼

1?? 創(chuàng)建狀態(tài)管理庫:store.js

import React, { createContext, useReducer, useContext } from 'react';// 定義初始狀態(tài)
const initialState = {count: 0,user: null,
};// 定義 reducer
function reducer(state, action) {switch (action.type) {case 'INCREMENT':return { ...state, count: state.count + 1 };case 'DECREMENT':return { ...state, count: state.count - 1 };case 'SET_USER':return { ...state, user: action.payload };default:throw new Error(`Unknown action type: ${action.type}`);}
}// 創(chuàng)建兩個上下文
const StateContext = createContext(null);
const DispatchContext = createContext(null);// 創(chuàng)建 Provider
export function GlobalProvider({ children }) {const [state, dispatch] = useReducer(reducer, initialState);return (<StateContext.Provider value={state}><DispatchContext.Provider value={dispatch}>{children}</DispatchContext.Provider></StateContext.Provider>);
}// 封裝 hooks
export function useGlobalState() {const context = useContext(StateContext);if (context === undefined) {throw new Error('useGlobalState must be used within a GlobalProvider');}return context;
}export function useGlobalDispatch() {const context = useContext(DispatchContext);if (context === undefined) {throw new Error('useGlobalDispatch must be used within a GlobalProvider');}return context;
}

2?? 使用示例

App.js
import React from 'react';
import { GlobalProvider } from './store';
import Counter from './Counter';
import User from './User';function App() {return (<GlobalProvider><h1>My App</h1><Counter /><User /></GlobalProvider>);
}export default App;

Counter.js
import React from 'react';
import { useGlobalState, useGlobalDispatch } from './store';function Counter() {const { count } = useGlobalState();const dispatch = useGlobalDispatch();return (<div><h2>Count: {count}</h2><button onClick={() => dispatch({ type: 'INCREMENT' })}>+</button><button onClick={() => dispatch({ type: 'DECREMENT' })}>-</button></div>);
}export default Counter;

User.js
import React from 'react';
import { useGlobalState, useGlobalDispatch } from './store';function User() {const { user } = useGlobalState();const dispatch = useGlobalDispatch();const login = () => {dispatch({ type: 'SET_USER', payload: { name: 'Heo Hao' } });};return (<div><h2>User: {user ? user.name : 'Guest'}</h2><button onClick={login}>Login</button></div>);
}export default User;

🧩 優(yōu)點

  • 不依賴第三方庫(如 Redux、Zustand)
  • 輕量、易用、類型安全(可結(jié)合 TS)
  • 適合中小項目
http://www.risenshineclean.com/news/385.html

相關(guān)文章:

  • 10個值得推薦的免費設(shè)計網(wǎng)站/怎么讓網(wǎng)站快速收錄
  • photoshop在線修圖/東莞seo管理
  • 門戶網(wǎng)站樣式/百度提問在線回答問題
  • 網(wǎng)站聯(lián)系我們的地圖怎么做的/seo收費標(biāo)準(zhǔn)
  • 北京網(wǎng)站建設(shè)交易/肇慶seo優(yōu)化
  • 網(wǎng)站 色調(diào)/世界搜索引擎大全
  • 南充網(wǎng)站建設(shè)工作室/互聯(lián)網(wǎng)推廣有哪些方式
  • 工業(yè)和信息化部icp網(wǎng)站備案系統(tǒng)/百度查詢?nèi)肟?/a>
  • 游戲推廣網(wǎng)站如何做的/一件代發(fā)48個貨源網(wǎng)站
  • 淘寶客怎么建設(shè)網(wǎng)站/實時新聞
  • 姜堰 萬邦建設(shè)集團(tuán)網(wǎng)站/seosem是什么職位
  • 信融網(wǎng)站建設(shè)網(wǎng)站開發(fā)/百度推廣代運營公司
  • 做網(wǎng)站傻瓜/汕頭企業(yè)網(wǎng)絡(luò)推廣
  • 手機(jī)網(wǎng)站建設(shè)方案書/網(wǎng)店推廣渠道有哪些
  • 微信云網(wǎng)站用什么做/長沙網(wǎng)站托管優(yōu)化
  • 做網(wǎng)站和網(wǎng)頁區(qū)別/seo黑帽教學(xué)網(wǎng)
  • 在那個網(wǎng)站可買做鞋子的羊毛/免費網(wǎng)站代理訪問
  • 深圳網(wǎng)絡(luò)??凭W(wǎng)站建設(shè)/百度推廣需要多少錢
  • 天津智能網(wǎng)站建設(shè)費用/公司網(wǎng)址
  • 百度怎么做自己的網(wǎng)站/百度知道問答平臺
  • 網(wǎng)站名稱是什么/網(wǎng)站優(yōu)化網(wǎng)絡(luò)推廣seo
  • 佛山網(wǎng)站建設(shè)科技公司/手機(jī)助手
  • 網(wǎng)站制作公司網(wǎng)站建設(shè)/chrome官網(wǎng)下載
  • 淘寶導(dǎo)航里的鏈接網(wǎng)站怎么做/百度建站
  • 網(wǎng)站建設(shè)系統(tǒng)開發(fā)需要多少錢/如何提高seo關(guān)鍵詞排名
  • php的網(wǎng)站/新聞頭條新聞
  • 昆明網(wǎng)站建設(shè)培訓(xùn)/寧德市政府
  • 網(wǎng)站建設(shè)制作設(shè)計營銷公司四川/寧波seo網(wǎng)絡(luò)推廣優(yōu)質(zhì)團(tuán)隊
  • 深圳網(wǎng)站建設(shè)定制開發(fā)/成都百度seo優(yōu)化公司
  • 不能制作網(wǎng)頁的軟件是/邯鄲網(wǎng)站建設(shè)優(yōu)化