22-Redux-1
创始人
2024-01-25 06:48:00

 


//npm init
//npm install redux
//1 导入redux(不能通过es6的方式)
// commonjs一种 -> node.jsconst redux = require('redux')const initialState = {counter: 0
}
// reducer
function reducer(state = initialState, action) {switch(action.type) {case "INCREMENT":return { ...state, counter: state.counter + 1 }case "DECREMENT":return { ...state, counter: state.counter - 1 }case "ADD_NUMBER":return { ...state, counter: state.counter + action.num }case "SUB_NUMBER":return { ...state, counter: state.counter - action.num }default:return state}
}//store
const store = redux.createStore(reducer)//订阅store的修改
store.subscribe(() => {console.log("state发生了改变", "counter:", store.getState().counter)
})//actions
const action1 = { type: "INCREMENT" }
const action2 = { type: "DECREMENT" }
const action3 = { type: "ADD_NUMBER", num: 5 }
const action4 = { type: "SUB_NUMBER", num: 12 }//派发 action
store.dispatch(action1)
store.dispatch(action2)
store.dispatch(action3)
store.dispatch(action4)

升级版本脱离react直接使用

 index.js

import store from './store/index.js'
import { addAction, subAction, inAction, deAction } from './store/actionCreators.js'store.subscribe(() => {console.log(store.getState())
})store.dispatch(addAction(10))
store.dispatch(subAction(5))store.dispatch(inAction())
store.dispatch(deAction())

store文件夹

actionCreator.js

 


import {  ADD_NUMBER, SUB_NUMBER, INCREMENT, DECREMENT   } from './constants.js'export const addAction = (num) => {return {type: ADD_NUMBER,num,}
}
export const subAction = (num) => {return {type: SUB_NUMBER,num,}
}export const inAction = () => {return {type: INCREMENT,}
}export const deAction = () => {return {type: DECREMENT,}
}

constants.js

export const ADD_NUMBER = "ADD_NUMBER";
export const SUB_NUMBER = "SUB_NUMBER";
export const INCREMENT = "INCREMENT";
export const DECREMENT = "DECREMENT";

index.js

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

reducer.js

 

import {  ADD_NUMBER, SUB_NUMBER, INCREMENT, DECREMENT   } from './constants.js'const defaultState = {counter: 0
}function reducer(state = defaultState, action) {switch(action.type) {case ADD_NUMBER:return { ...state, counter: state.counter + action.num }case SUB_NUMBER:return { ...state, counter: state.counter - action.num }case INCREMENT:return { ...state, counter: state.counter + 1 }case DECREMENT:return { ...state, counter: state.counter - 1 }default:return state}
}export default reducer

 在react中引用index.js

 

相关内容

热门资讯

什么是419 419是什么意思... 嗨,辣条陪你理智吃瓜最近刚还想着10月特殊时期可能没啥瓜了,结果一到月底就来了。孟美岐冷不丁就突然上...
埃菲尔铁塔在哪 中国仿建埃菲尔... 2019年4月26日,广西南宁市,街头惊现一座巨型山寨版埃菲尔铁塔,高约20米,白色塔身,造型逼真,...
苗族的传统节日 贵州苗族节日有... 【岜沙苗族芦笙节】岜沙,苗语叫“分送”,距从江县城7.5公里,是世界上最崇拜树木并以树为神的枪手部落...
北京的名胜古迹 北京最著名的景... 北京从元代开始,逐渐走上帝国首都的道路,先是成为大辽朝五大首都之一的南京城,随着金灭辽,金代从海陵王...
应用未安装解决办法 平板应用未... ---IT小技术,每天Get一个小技能!一、前言描述苹果IPad2居然不能安装怎么办?与此IPad不...
脚上的穴位图 脚面经络图对应的... 人体穴位作用图解大全更清晰直观的标注了各个人体穴位的作用,包括头部穴位图、胸部穴位图、背部穴位图、胳...
长白山自助游攻略 吉林长白山游... 昨天介绍了西坡的景点详细请看链接:一个人的旅行,据说能看到长白山天池全凭运气,您的运气如何?今日介绍...
猫咪吃了塑料袋怎么办 猫咪误食... 你知道吗?塑料袋放久了会长猫哦!要说猫咪对塑料袋的喜爱程度完完全全可以媲美纸箱家里只要一有塑料袋的响...
世界上最漂亮的人 世界上最漂亮... 此前在某网上,选出了全球265万颜值姣好的女性。从这些数量庞大的女性群体中,人们投票选出了心目中最美...
埃菲尔铁塔在哪 中国仿建埃菲尔... 2019年4月26日,广西南宁市,街头惊现一座巨型山寨版埃菲尔铁塔,高约20米,白色塔身,造型逼真,...
什么是419 419是什么意思... 嗨,辣条陪你理智吃瓜最近刚还想着10月特殊时期可能没啥瓜了,结果一到月底就来了。孟美岐冷不丁就突然上...
苗族的传统节日 贵州苗族节日有... 【岜沙苗族芦笙节】岜沙,苗语叫“分送”,距从江县城7.5公里,是世界上最崇拜树木并以树为神的枪手部落...
北京的名胜古迹 北京最著名的景... 北京从元代开始,逐渐走上帝国首都的道路,先是成为大辽朝五大首都之一的南京城,随着金灭辽,金代从海陵王...