Skip to content

Commit a85e225

Browse files
author
lewis617
committed
zhushi
1 parent a222d9a commit a85e225

File tree

2 files changed

+13
-6
lines changed

2 files changed

+13
-6
lines changed

redux-examples/async/actions/index.js

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,28 +4,28 @@ export const REQUEST_POSTS = 'REQUEST_POSTS'
44
export const RECEIVE_POSTS = 'RECEIVE_POSTS'
55
export const SELECT_REDDIT = 'SELECT_REDDIT'
66
export const INVALIDATE_REDDIT = 'INVALIDATE_REDDIT'
7-
7+
//选择新闻类型action
88
export function selectReddit(reddit) {
99
return {
1010
type: SELECT_REDDIT,
1111
reddit
1212
}
1313
}
14-
14+
//废弃新闻类型action
1515
export function invalidateReddit(reddit) {
1616
return {
1717
type: INVALIDATE_REDDIT,
1818
reddit
1919
}
2020
}
21-
21+
//开始获取action
2222
function requestPosts(reddit) {
2323
return {
2424
type: REQUEST_POSTS,
2525
reddit
2626
}
2727
}
28-
28+
//获取成功的action
2929
function receivePosts(reddit, json) {
3030
return {
3131
type: RECEIVE_POSTS,
@@ -35,6 +35,7 @@ function receivePosts(reddit, json) {
3535
}
3636
}
3737

38+
//获取文章,先触发开始获取action,完成后触发获取成功的action
3839
function fetchPosts(reddit) {
3940
return dispatch => {
4041
dispatch(requestPosts(reddit))
@@ -44,6 +45,7 @@ function fetchPosts(reddit) {
4445
}
4546
}
4647

48+
//是否需要获取文章
4749
function shouldFetchPosts(state, reddit) {
4850
const posts = state.postsByReddit[reddit]
4951
if (!posts) {
@@ -55,6 +57,7 @@ function shouldFetchPosts(state, reddit) {
5557
return posts.didInvalidate
5658
}
5759

60+
//如果需要则开始获取文章
5861
export function fetchPostsIfNeeded(reddit) {
5962
return (dispatch, getState) => {
6063
if (shouldFetchPosts(getState(), reddit)) {

redux-examples/async/reducers/index.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import {
44
REQUEST_POSTS, RECEIVE_POSTS
55
} from '../actions'
66

7+
//选择新闻后,将state.selectedReddit设为所选选项
78
function selectedReddit(state = 'reactjs', action) {
89
switch (action.type) {
910
case SELECT_REDDIT:
@@ -14,8 +15,11 @@ function selectedReddit(state = 'reactjs', action) {
1415
}
1516

1617
function posts(state = {
18+
//是否正在获取最新
1719
isFetching: false,
20+
//是否废弃
1821
didInvalidate: false,
22+
//内容
1923
items: []
2024
}, action) {
2125
switch (action.type) {
@@ -39,7 +43,7 @@ function posts(state = {
3943
return state
4044
}
4145
}
42-
46+
//废弃、接收到、开始接受新闻后,将state.postsByReddit设为相关参数
4347
function postsByReddit(state = { }, action) {
4448
switch (action.type) {
4549
case INVALIDATE_REDDIT:
@@ -52,7 +56,7 @@ function postsByReddit(state = { }, action) {
5256
return state
5357
}
5458
}
55-
59+
//将两个reducer合并成一个reducer,也就将全局的state加上postsByReddit,selectedReddit两个属性,每个属性都有自己的state
5660
const rootReducer = combineReducers({
5761
postsByReddit,
5862
selectedReddit

0 commit comments

Comments
 (0)