Skip to content

Commit

Permalink
chore:add a debug example
Browse files Browse the repository at this point in the history
  • Loading branch information
L-Qun committed Oct 2, 2024
1 parent 8847bfd commit 6d68894
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 0 deletions.
39 changes: 39 additions & 0 deletions examples/redux/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
const { createStore } = require('redux')

const initialState = {
count: 0,
}

const INCREMENT = 'INCREMENT'
const DECREMENT = 'DECREMENT'

function increment() {
return { type: INCREMENT }
}

function decrement() {
return { type: DECREMENT }
}

function counterReducer(state = initialState, action) {
switch (action.type) {
case INCREMENT:
return { count: state.count + 1 }
case DECREMENT:
return { count: state.count - 1 }
default:
return state
}
}

// 创建 store
const store = createStore(counterReducer)

// 订阅 store,当状态发生变化时执行回调函数
store.subscribe(() => {
console.log('当前状态:', store.getState())
})

store.dispatch(increment()) // 当前状态: { count: 1 }
store.dispatch(increment()) // 当前状态: { count: 2 }
store.dispatch(decrement()) // 当前状态: { count: 1 }
22 changes: 22 additions & 0 deletions examples/redux/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 15 additions & 0 deletions examples/redux/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"name": "redux",
"version": "1.0.0",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "",
"license": "ISC",
"description": "",
"dependencies": {
"redux": "^5.0.1"
}
}

0 comments on commit 6d68894

Please sign in to comment.