反应redux - `state.function [item]`语法

时间:2018-03-14 02:46:43

标签: reactjs redux

我正在尝试修改Redux的异步Reddit API example code

但是我很难理解这两个功能发生了什么:

//action
function shouldFetchPosts(state, subreddit) {
  const posts = state.postsBySubreddit[subreddit]
  if (!posts) {
    return true
  } else if (posts.isFetching) {
    return false
  } else {
    return posts.didInvalidate
  }
}

//reducer 
function postsBySubreddit(state = {}, action) {
  switch (action.type) {
    case INVALIDATE_SUBREDDIT:
    case RECEIVE_POSTS:
    case REQUEST_POSTS:
      return Object.assign({}, state, {
        [action.subreddit]: posts(state[action.subreddit], action)
      })
    default:
      return state
  }
}

特别是postsbySubreddit[subreddit];我从未见过使用下标符号访问的函数,并且该函数在调用时是否需要参数?

1 个答案:

答案 0 :(得分:1)

state.postsBySubreddit不是函数,它是商店中postsBySubreddit减速器函数存储其状态的属性的名称。

如果您的系统包含3个reducer,r1()r2()r3(),那么store将有3个属性,这些reducers存储其数据:{{1 },store.r1store.r2