Skip to content
Navigation Menu
Sign in
Appearance settings
Platform
AI CODE CREATION
GitHub Copilot
Write better code with AI
GitHub Copilot app
Direct agents from issue to merge
MCP Registry
Integrate external tools
DEVELOPER WORKFLOWS
Actions
Automate any workflow
Codespaces
Instant dev environments
Issues
Plan and track work
Code Review
Manage code changes
Code Quality
Enforce quality at merge
APPLICATION SECURITY
GitHub Advanced Security
Find and fix vulnerabilities
Code security
Secure your code as you build
Secret protection
Stop leaks before they start
EXPLORE
Why GitHub
Documentation
Blog
Changelog
Marketplace
View all features
Solutions
BY COMPANY SIZE
Enterprises
Small and medium teams
Startups
Nonprofits
BY USE CASE
App Modernization
DevSecOps
DevOps
CI/CD
View all use cases
BY INDUSTRY
Healthcare
Financial services
Manufacturing
Government
View all industries
View all solutions
Resources
EXPLORE BY TOPIC
AI
Software Development
DevOps
Security
View all topics
EXPLORE BY TYPE
Customer stories
Events & webinars
Ebooks & reports
Business insights
GitHub Skills
SUPPORT & SERVICES
Documentation
Customer support
Community forum
Trust center
Partners
View all resources
Open Source
COMMUNITY
GitHub Sponsors
Fund open source developers
PROGRAMS
Security Lab
Maintainer Community
GitHub Stars
Archive Program
REPOSITORIES
Topics
Trending
Collections
Enterprise
ENTERPRISE SOLUTIONS
Enterprise platform
AI-powered developer platform
AVAILABLE ADD-ONS
GitHub Advanced Security
Enterprise-grade security features
Copilot for Business
Enterprise-grade AI features
Premium Support
Enterprise-grade 24/7 support
Pricing
Search
/
Sign in
Sign up
Appearance settings
You signed in with another tab or window.
Reload
to refresh your session.
You signed out in another tab or window.
Reload
to refresh your session.
You switched accounts on another tab or window.
Reload
to refresh your session.
Dismiss alert
{{ message }}
Uh oh!
There was an error while loading.
Please reload this page
.
react-guide
/
redux-tutorial-cn
Public
Notifications
You must be signed in to change notification settings
Fork
290
Star
1.9k
Code
Issues
4
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Actions
Projects
Security and quality
Insights
Files
Expand file tree
master
Breadcrumbs
redux-tutorial-cn
/
03_simple-reducer.js
Copy path
Blame
More file actions
Blame
More file actions
Latest commit
History
History
History
44 lines (30 loc) · 1.94 KB
master
Breadcrumbs
redux-tutorial-cn
/
03_simple-reducer.js
Copy path
Top
File metadata and controls
Code
Blame
44 lines (30 loc) · 1.94 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
// 章节 3 - simple-reducer.js
// 现在,我们知道如何去创建一个 Redux 实例,并让它管理应用中的 state
// 下面讲一下这些 reducer 函数是如何转换 state 的。
// Reducer 与 Store 区别:
// 你可能已经注意到,在简介章节中的 Flux 图表中,有 Store,但没有
// Redux 中的 Reducer。那么,Store 与 Reducer 到底有哪些区别呢?
// 实际的区别比你想象的简单:Store 可以保存你的 data,而 Reducer 不能。
// 因此在传统的 Flux 中,Store 本身可以保存 state,但在 Redux 中,每次调用 reducer
// 时,都会传入待更新的 state。这样的话,Redux 的 store 就变成了
// “无状态的 store” 并且改了个名字叫 Reducer。
// 如上所述,在创建一个 Redux 实例前,需要给它一个 reducer 函数...
import { createStore } from 'redux'
var store_0 = createStore(() => {})
// ...所以每当一个 action 发生时,Redux 都能调用这个函数。
// 往 createStore 传 Reducer 的过程就是给 Redux 绑定 action 处理函数(也就是 Reducer)的过程。
// action 处理函数在 01_simple-action-creator.js 章节中有讨论过。
// 在 Reducer 中打印一些 log
var reducer = function (...args) {
console.log('Reducer was called with args', args)
}
var store_1 = createStore(reducer)
// 输出:Reducer was called with args [ undefined, { type: '@@redux/INIT' } ]
// 看出来了吗?我们的 reducer 被调用了,但我们并没有 dispatch 任何 action...
// 这是因为在初始化应用 state 的时候,
// Redux dispatch 了一个初始化的 action ({ type: '@@redux/INIT' })
// 在被调用时,一个 reducer 会得到这些参数:(state, action)
// 在应用初始化时,state 还没被初始化,因此它的值是 "undefined",
// 这是非常符合逻辑的
// 在处理 “init” action 之后,我们应用中的 state 又会是怎么样的呢?
// 下一章节:04_get-state.js
You can’t perform that action at this time.