当前位置: 首页 > news >正文

盐城市亭湖区建设局网站/博客可以做seo吗

盐城市亭湖区建设局网站,博客可以做seo吗,南通通州建设工程质量监督网站,怎样建立自己网站多少钱使用 useContext useReducer 实现的轻量级状态管理,适合中小型 React 应用使用。 🧠 实现思路 使用 createContext 创建两个上下文:StateContext 和 DispatchContext。用 useReducer 管理状态逻辑。创建一个 Provider 组件包裹应用。提供两…

使用 useContext + useReducer 实现的轻量级状态管理,适合中小型 React 应用使用。


🧠 实现思路

  1. 使用 createContext 创建两个上下文:StateContextDispatchContext
  2. useReducer 管理状态逻辑。
  3. 创建一个 Provider 组件包裹应用。
  4. 提供两个 hooks:useGlobalState()useGlobalDispatch(),分别获取状态和派发方法。

✅ 示例代码

1️⃣ 创建状态管理库:store.js

import React, { createContext, useReducer, useContext } from 'react';// 定义初始状态
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}`);}
}// 创建两个上下文
const StateContext = createContext(null);
const DispatchContext = createContext(null);// 创建 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;

🧩 优点

  • 不依赖第三方库(如 Redux、Zustand)
  • 轻量、易用、类型安全(可结合 TS)
  • 适合中小项目
http://www.jinmujx.cn/news/385.html

相关文章:

  • 10个值得推荐的免费设计网站/怎么让网站快速收录
  • photoshop在线修图/东莞seo管理
  • 门户网站样式/百度提问在线回答问题
  • 网站联系我们的地图怎么做的/seo收费标准
  • 北京网站建设交易/肇庆seo优化
  • 网站 色调/世界搜索引擎大全
  • 南充网站建设工作室/互联网推广有哪些方式
  • 工业和信息化部icp网站备案系统/百度查询入口
  • 游戏推广网站如何做的/一件代发48个货源网站
  • 淘宝客怎么建设网站/实时新闻
  • 姜堰 万邦建设集团网站/seosem是什么职位
  • 信融网站建设网站开发/百度推广代运营公司
  • 做网站傻瓜/汕头企业网络推广
  • 手机网站建设方案书/网店推广渠道有哪些
  • 微信云网站用什么做/长沙网站托管优化
  • 做网站和网页区别/seo黑帽教学网
  • 在那个网站可买做鞋子的羊毛/免费网站代理访问
  • 深圳网络专科网站建设/百度推广需要多少钱
  • 天津智能网站建设费用/公司网址
  • 百度怎么做自己的网站/百度知道问答平台
  • 网站名称是什么/网站优化网络推广seo
  • 佛山网站建设科技公司/手机助手
  • 网站制作公司网站建设/chrome官网下载
  • 淘宝导航里的链接网站怎么做/百度建站
  • 网站建设系统开发需要多少钱/如何提高seo关键词排名
  • php的网站/新闻头条新闻
  • 昆明网站建设培训/宁德市政府
  • 网站建设制作设计营销公司四川/宁波seo网络推广优质团队
  • 深圳网站建设定制开发/成都百度seo优化公司
  • 不能制作网页的软件是/邯郸网站建设优化