Skip to content

Commit b421e1f

Browse files
author
lewis617
committed
redux demo zhushi
1 parent d93d6b8 commit b421e1f

File tree

6 files changed

+24
-9
lines changed

6 files changed

+24
-9
lines changed
Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,38 @@
11
export const INCREMENT_COUNTER = 'INCREMENT_COUNTER'
22
export const DECREMENT_COUNTER = 'DECREMENT_COUNTER'
3-
3+
//导出加一的方法
44
export function increment() {
55
return {
66
type: INCREMENT_COUNTER
77
}
88
}
9-
9+
//导出减一的方法
1010
export function decrement() {
1111
return {
1212
type: DECREMENT_COUNTER
1313
}
1414
}
15-
15+
//导出奇数加一的方法,该方法返回一个方法,包含dispatch和getState两个参数,dispatch用于执行action的方法,getState返回state
1616
export function incrementIfOdd() {
1717
return (dispatch, getState) => {
18+
//获取state对象中的counter属性值
1819
const { counter } = getState()
1920

21+
//偶数则返回
2022
if (counter % 2 === 0) {
2123
return
2224
}
23-
25+
//没有返回就执行加一
2426
dispatch(increment())
2527
}
2628
}
27-
29+
//导出一个方法,包含一个默认参数delay,返回一个方法,一秒后加一
2830
export function incrementAsync(delay = 1000) {
2931
return dispatch => {
3032
setTimeout(() => {
3133
dispatch(increment())
3234
}, delay)
3335
}
3436
}
37+
38+
//这些方法都导出,在其他文件导入时候,使用import * as actions 就可以生成一个actions对象包含所有的export

redux-examples/counter/components/Counter.js

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@ import React, { Component, PropTypes } from 'react'
22

33
class Counter extends Component {
44
render() {
5-
const { increment, incrementIfOdd, incrementAsync, decrement, counter } = this.props
5+
//从组件的props属性中导入四个方法和一个变量
6+
const { increment, incrementIfOdd, incrementAsync, decrement, counter } = this.props;
7+
//渲染组件,包括一个数字,四个按钮
68
return (
79
<p>
810
Clicked: {counter} times
@@ -18,13 +20,15 @@ class Counter extends Component {
1820
)
1921
}
2022
}
21-
23+
//限制组件的props安全
2224
Counter.propTypes = {
25+
//increment必须为fucntion,且必须存在
2326
increment: PropTypes.func.isRequired,
2427
incrementIfOdd: PropTypes.func.isRequired,
2528
incrementAsync: PropTypes.func.isRequired,
2629
decrement: PropTypes.func.isRequired,
30+
//counter必须为数字,且必须存在
2731
counter: PropTypes.number.isRequired
28-
}
32+
};
2933

3034
export default Counter

redux-examples/counter/containers/App.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,16 @@ import { connect } from 'react-redux'
33
import Counter from '../components/Counter'
44
import * as CounterActions from '../actions/counter'
55

6+
//将state.counter绑定到props的counter
67
function mapStateToProps(state) {
78
return {
89
counter: state.counter
910
}
1011
}
11-
12+
//将action的所有方法绑定到props上
1213
function mapDispatchToProps(dispatch) {
1314
return bindActionCreators(CounterActions, dispatch)
1415
}
1516

17+
//通过react-redux提供的connect方法将我们需要的state中的数据和actions中的方法绑定到props上
1618
export default connect(mapStateToProps, mapDispatchToProps)(Counter)

redux-examples/counter/reducers/counter.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { INCREMENT_COUNTER, DECREMENT_COUNTER } from '../actions/counter'
22

3+
//reducer其实也是个方法而已,参数是state和action,返回值是新的state
34
export default function counter(state = 0, action) {
45
switch (action.type) {
56
case INCREMENT_COUNTER:

redux-examples/counter/reducers/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { combineReducers } from 'redux'
22
import counter from './counter'
33

4+
//使用redux的combineReducers方法将所有reducer打包起来
45
const rootReducer = combineReducers({
56
counter
67
})

redux-examples/counter/store/configureStore.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,16 @@ import { createStore, applyMiddleware } from 'redux'
22
import thunk from 'redux-thunk'
33
import reducer from '../reducers'
44

5+
//applyMiddleware来自redux可以包装 store 的 dispatch
6+
//thunk作用是使被 dispatch 的 function 会接收 dispatch 作为参数,并且可以异步调用它
57
const createStoreWithMiddleware = applyMiddleware(
68
thunk
79
)(createStore)
810

911
export default function configureStore(initialState) {
1012
const store = createStoreWithMiddleware(reducer, initialState)
1113

14+
//热替换选项
1215
if (module.hot) {
1316
// Enable Webpack hot module replacement for reducers
1417
module.hot.accept('../reducers', () => {

0 commit comments

Comments
 (0)