Skip to content

Commit 78a3359

Browse files
committed
Merge branch 'sources' of github.com:wangbinyq/wangbinyq.github.io into sources
2 parents 079a59f + 55001c4 commit 78a3359

File tree

1 file changed

+108
-0
lines changed

1 file changed

+108
-0
lines changed

source/_drafts/redux-usage.md

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
---
2+
title: redux-usage
3+
tags:
4+
---
5+
6+
# Introduction
7+
8+
## Motivation
9+
10+
- state manage
11+
- mutation and asynchronicity
12+
13+
## Core Concepts
14+
15+
- state: model without setters
16+
- action: change something in state (what happend)
17+
- reducer: to tie state and action together `(state, action) => state` (how state change)
18+
- split state and reducer to small part, and then combine them together.
19+
20+
## Three Principles
21+
22+
- Single source of truth
23+
- eaiser to debug and inspect
24+
- persist app's state
25+
- State is read-only
26+
- any view or network callbacks need make a intent to change state
27+
- changes are cenralized and one by one in a strict order(no race conditions)
28+
- actions can be logged, serialized, stored, and replayed
29+
- Changes are made with pure functions
30+
31+
## Prior Art
32+
33+
- Flux
34+
- Elm
35+
- Immutable
36+
- Baobab
37+
- RxJS
38+
39+
# Basic
40+
41+
## Actions
42+
43+
- payloads of information that send to app store
44+
- the only source of information for the store
45+
- actions are plain JavaScript objects that must have a `type` property
46+
- *action creators*
47+
- we do not dispatch when invoked
48+
- bound action creator that automatically dispatches
49+
- access `dispatch` by `store.dispatch` or `react-redux`'s `connect()`s
50+
- `bindActionCreators(Function | Object, Function) => Function | Object `
51+
- can be async and have side-effect
52+
53+
## Reducers
54+
55+
- state shape: data stae + UI state
56+
- keep state as normalized as possible, without any nesting.
57+
- `todosById: { id -> todo }` and `todos: array<id>`
58+
- pure function: `(previousState, action) => newState`, no pure operations:
59+
- Mutate its argmuents
60+
- side effect
61+
- non-pure function, like `Date.now()` or `Math.random()`
62+
- do not mutate the state, use `Object.assign({}, ...)` or ES6 `object spread operator`
63+
- return previous `state` in the `default` case
64+
- splitting reducers:
65+
- reducer composition: `combineReducers()`
66+
67+
## Store
68+
69+
- holds application state
70+
- allows access to state via `getState()`
71+
- allows state to be updated via `dispatch(action)`
72+
- registers listeners via `subscribe(listener)`
73+
- handles unregistering of listeners via the function returned by `subscribe(listener)`
74+
75+
## DataFlow
76+
77+
- strict unidirectional data flow
78+
79+
80+
## Usage with React
81+
82+
- presentational and container components
83+
84+
| | Presentational Components | Container Components |
85+
|---|---|---|
86+
| Purpose | How things look (markup, styles) | How things work()data fetching, state updates |
87+
| Aware of Redux | No | Yes |
88+
| To read data | Read data from props | Subscribe to Redux state |
89+
| To change data | Invoke callbacks from props | Dispatch Redux actions |
90+
| Are written | By hand | Usually generated by React Redux |
91+
92+
- `connect({mapStateToProps, mapDispatchToProps})`
93+
- `<Provider>`
94+
- `context`
95+
96+
## Async Actions
97+
98+
- need middleware to deal async actions
99+
- default dispatch can only deal plain object actions.
100+
- middleware wrapper store's `dispatch()` make it able to deal with async actions
101+
- async actions creators
102+
- redux-thunk: dispatch function action
103+
- redux-promise: dispatch promise action
104+
105+
## Middleware
106+
107+
- compose `dispatch`
108+
- action -- dispatch (middleware1 --> middleware2 ... --> middlewaren) --> reducer

0 commit comments

Comments
 (0)