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

个人网站备案费用重庆专业做淘宝网站

个人网站备案费用,重庆专业做淘宝网站,濮阳做网站推广的公司,深圳市凡客科技有限公司核心思想:单一数据源、状态是只读的、以及使用纯函数更新状态。 组成部分 Store(存储) 应用的唯一状态容器,存储整个应用的状态树,使用 createStore() 创建。 getState():获取当前状态。dispatch(action)&#xff…

核心思想:单一数据源状态是只读的、以及使用纯函数更新状态。

组成部分

Store(存储)

应用的唯一状态容器,存储整个应用的状态树,使用 createStore() 创建。

  1. getState():获取当前状态。
  2. dispatch(action):派发动作以触发状态更新。
  3. subscribe(listener):订阅状态变化。

Action(动作)

描述应用中发生的事情,就是一个普通的 JavaScript 对象,必须有 type 属性,用来描述动作的类型,其他字段可以包含任何附加信息(例如,payload)。

Reducer(状态处理器)

是一个纯函数,接收当前的状态和一个动作,返回新的状态,函数签名:(state, action) => newState,通过处理不同类型的动作更新状态。

Middleware(中间件)

拦截 dispatch 的动作,用于扩展 Redux 的功能,例如处理异步操作redux-thunk)或日志记录(redux-logger)。

Provider(React 集成部分)

使用 react-redux 提供 Provider 组件,将 Redux 的 store 传递给整个 React 组件树。

应用

安装依赖

npm install redux react-redux

创建 Redux Store

定义Action

// src/actions/types.js
// 定义一些常量来表示 action 的类型,这有助于避免拼写错误
export const INCREMENT = "INCREMENT";
export const DECREMENT = "DECREMENT";
// src/actions/counterActions.js
// 创建 action creator 函数来生成 action 对象。
import { INCREMENT, DECREMENT } from "./types";export const increment = () => ({type: INCREMENT,
});export const decrement = () => ({type: DECREMENT,
});

创建 Reducer

// src/reducers/counterReducer.js
// 创建 reducer 函数来处理不同的 action 类型。
import { INCREMENT, DECREMENT } from "../actions/types";const initialState = {count: 0,
};function counterReducer(state = initialState, action) {switch (action.type) {case INCREMENT:return {...state,count: state.count + 1,};case DECREMENT:return {...state,count: state.count - 1,};default:return state;}
}export default counterReducer;
// src/reducers/index.js
import { combineReducers } from "redux";
import counterReducer from "./counterReducer";const rootReducer = combineReducers({counter: counterReducer,
});export default rootReducer;

创建 Store

// store.js
import { createStore } from 'redux';
import counterReducer from './reducer';const store = createStore(counterReducer);export default store;

在 React 应用中使用 Redux

设置 Provider

// index.js
import React from "react";
import ReactDOM from "react-dom/client";
import App from "./App";
// Provider是React-Redux提供的一个组件,用于将Redux store传递给应用中的所有组件。
import { Provider } from "react-redux";
import store from "./store/index";
import reportWebVitals from "./reportWebVitals";const root = ReactDOM.createRoot(document.getElementById("root"));
root.render(<React.StrictMode><Provider store={store}>{" "}{/* 将App组件包裹在Provider中 */}<App /></Provider></React.StrictMode>
);
reportWebVitals();

组件中访问状态和派发动作

// src/components/js/CounterWithHooks.js
import React from "react";
import { useSelector, useDispatch } from "react-redux";
import { increment, decrement } from "../../actions/counterActions";function CounterWithHooks() {const count = useSelector((state) => state.counter.count);const dispatch = useDispatch();return (<div><h1>Count: {count}</h1><button onClick={() => dispatch(increment())}>Increment</button><button onClick={() => dispatch(decrement())}>Decrement</button></div>);
}export default CounterWithHooks;

Redux 中间件的扩展

添加异步支持(redux-thunk

npm install redux-thunk

修改 store.js

import { createStore, applyMiddleware } from 'redux';
import {thunk}  from 'redux-thunk';// 使用命名导出
// import thunk  from 'redux-thunk';// 使用默认导出
import counterReducer from './reducer';const store = createStore(counterReducer, applyMiddleware(thunk));export default store;

示例异步 Action

// src/actions/counterActions.js
// 创建 action creator 函数来生成 action 对象。
import { INCREMENT, DECREMENT } from "./types";
export const increment = () => ({type: INCREMENT,
});export const decrement = () => ({type: DECREMENT,
});export const incrementAsync = () => {return (dispatch) => {setTimeout(() => {dispatch(increment());}, 1000);};
};export const decrementAsync = () => {return (dispatch) => {setTimeout(() => {dispatch(decrement());}, 1000);};
};

示例

// src/components/js/CounterWithHooks.js
import React from "react";
import { useSelector, useDispatch } from "react-redux";
import { increment, decrement, incrementAsync, decrementAsync } from "../../actions/counterActions";function CounterWithHooks() {const count = useSelector((state) => state.counter.count);const dispatch = useDispatch();return (<div><h1>Count: {count}</h1><button onClick={() => dispatch(increment())}>Increment</button><button onClick={() => dispatch(decrement())}>Decrement</button><button onClick={() => dispatch(incrementAsync())}>Increment Async</button><button onClick={() => dispatch(decrementAsync())}>Decrement Async</button></div>);
}export default CounterWithHooks;

总结

  1. State: 整个应用的状态树
  2. Actions: 描述状态变化的对象
  3. Reducers: 纯函数,根据 action 更新 state。
  4. Store: 持有应用的 state 树,提供方法来获取 state、分发 action 和注册/注销监听器。
http://www.yayakq.cn/news/620626/

相关文章:

  • wordpress 好的相册seo优化工具有哪些
  • 南昌网站建设培训学校山西seo推广系统
  • 襄阳哪里做网站企业网站开发成都
  • 长春老火车站图片推广什么app佣金高
  • 手机网站全屏代码ui设计的网站
  • 苏州专业网站制作方案做演示的网站
  • 网站构建的过程网站优化的好处
  • 网站形式影视公司网页设计
  • 免费建立网站的平台高德地图在英国可以用吗
  • 厦门网站建设设计2021中国企业500强
  • 注册网站花的钱做会计分录livezilla wordpress
  • 建设网站q8555 3807更换wordpress界面
  • asp网站 seo门户网站免费奖励自己
  • 网站建设会计搜狗短网址生成
  • HTML可以做彩票网站吗搜索引擎网站的搜素结果有何区别
  • 网站的开发流程可以分为哪三个阶段软件行业 网站建设 模块
  • 做的网站访问不了深圳动画设计制作哪些类型
  • 告诉你做网站需要多少钱手机上怎么做能打开的网站
  • 利用网络媒体营销来做电商网站论文vue.js wordpress
  • 如何建设好一个公司网站服务器维护工程师
  • 西安小程序开发公司哪家好温州最好的seo
  • 电脑当网站空间深圳宝安区什么时候解封
  • 网站建设整个流程东营专业网站建设
  • 苏州 网站建设 app做网站每天更新两篇文章
  • 微网站开发技术架构网站备案的好处有哪些
  • 建筑行业网站运营方案聚合广告联盟
  • 网站做多大尺寸精品网站建设费用 都来磐石网络
  • 如何查询某个网站的设计公司做ppt的模板的网站有哪些内容
  • 手机网站设计公司哪家专业漳州公司注册
  • 网站建设教程主页网站建设如何做