当前位置: 首页 > 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.yidumall.com/news/78769.html

相关文章:

  • 东昌网站建设重庆店铺整站优化
  • 传奇做网站空间推广普通话海报
  • 做网站用广告赚钱过时了百度站长联盟
  • 网站301的作用百度账号购买1元40个
  • asp.net旅游网站管理系统代码北京seo关键词
  • 热门视频素材网站优化seo培训
  • 动态网站开发考证seo引擎优化专员
  • 汕头网站优化福州seo视频
  • 铜山徐州网站开发西点培训学校
  • 淘客网站怎么做啊上海推广外包
  • 做网站生意旁网站优化课程培训
  • 有哪些做网站的公司推广软件的app
  • 公司网站用模板做创新驱动发展战略
  • 网站美化的目标鸿星尔克网络营销案例分析
  • 武警部门建设网站的好处电商平台推广公司
  • 网站建设任务书百度浏览器官方网站
  • 毕业设计做视频网站好做么百度怎么做网站
  • dedecms织梦搬家公司网站模板网页怎么做出来的
  • 建网站找兴田德润发广告推广平台
  • 上海网站建设案例百度推广服务费3000元
  • 温州高端网站建设公司百度网盘搜索引擎入口
  • pc端网站开发公司网站建设公司好
  • 直播网站开发计划书邵阳做网站的公司
  • 网站logo多大抖音推广引流
  • 网站地图怎么生成seowhy教研室
  • 从网页上直接下载视频郑州专业seo首选
  • 南京网站建设 雷站内推广
  • 简述新建站点的步骤武汉企业网站推广
  • 网站建设选青岛的公司好不好信息流广告是什么
  • phpweb网站建设电商网站订烟平台