javascript-嵌套的redux减速器

是否可以组合嵌套有以下结构的减速器:

import 'user' from ...
import 'organisation' from ...
import 'auth' from ...
// ...

export default combineReducers({
  auth: {
    combineReducers({
        user,
        organisation,  
    }),
    auth,
  },
  posts,
  pages,
  widgets,
  // .. more state here
});

国家的结构如下:

{
    auth: {
        user: {
            firstName: 'Foo',
            lastName: 'bar',
        }
        organisation: {
            name: 'Foo Bar Co.'
            phone: '1800-123-123',
        },
        token: 123123123,
        cypher: '256',
        someKey: 123,
    }
}

其中...auth减速器具有以下结构:

{
    token: 123123123,
    cypher: '256',
    someKey: 123,   
}

所以也许散布算子很方便吗? ...auth不确定:-(

4个解决方案
65 votes

最好使用authReducer组合嵌套的减速器。但是还有另一种非常有用的模式:嵌套的减速器。

const initialState = {
  user: null,
  organisation: null,
  token: null,
  cypher: null,
  someKey: null,
}

function authReducer(state = initialState, action) {
  switch (action.type) {
    case SET_ORGANISATION:
      return {...state, organisation: organisationReducer(state.organisation, action)}

    case SET_USER:
      return {...state, user: userReducer(state.user, action)}

    case SET_TOKEN:
      return {...state, token: action.token}

    default:
      return state
  }
}

在上面的示例中,authReducer可以将操作转发到organisationReduceruserReducer以更新其状态的某些部分。

Florent answered 2020-01-14T12:01:43Z
40 votes

只是想详细说明@Florent给出的非常好的答案,并指出您也可以通过将根减速器与也属于减速器的减速器组合在一起,以稍微不同的方式构造应用程序以实现嵌套减速器

例如

// src/reducers/index.js
import { combineReducers } from "redux";
import auth from "./auth";
import posts from "./posts";
import pages from "./pages";
import widgets from "./widgets";

export default combineReducers({
  auth,
  posts,
  pages,
  widgets
});

// src/reducers/auth/index.js
// note src/reducers/auth is instead a directory 
import { combineReducers } from "redux";
import organization from "./organization";
import user from "./user";
import security from "./security"; 

export default combineReducers({
  user,
  organization,
  security
});

这假定状态结构略有不同。 相反,像这样:

{
    auth: {
        user: {
            firstName: 'Foo',
            lastName: 'bar',
        }
        organisation: {
            name: 'Foo Bar Co.'
            phone: '1800-123-123',
        },
        security: {
            token: 123123123,
            cypher: '256',
            someKey: 123
        }
    },
    ...
}

如果您无法更改状态结构,则@Florent的方法可能会更好。

Joseph Nields answered 2020-01-14T12:02:17Z
6 votes

受到@florent答案的启发,我发现您也可以尝试一下。 不一定比他的回答更好,但我认为它稍微优雅一些。

function userReducer(state={}, action) {
    switch (action.type) {
    case SET_USERNAME:
      state.name = action.name;
      return state;
    default:
      return state;
  }
} 

function authReducer(state = {
  token: null,
  cypher: null,
  someKey: null,
}, action) {
  switch (action.type) {
    case SET_TOKEN:
      return {...state, token: action.token}
    default:
      // note: since state doesn't have "user",
      // so it will return undefined when you access it.
      // this will allow you to use default value from actually reducer.
      return {...state, user: userReducer(state.user, action)}
  }
}
D.W answered 2020-01-14T12:02:37Z
1 votes

示例(请参见下面的attachNestedReducers

import { attachNestedReducers } from './utils'
import { profileReducer } from './profile.reducer'
const initialState = { some: 'state' }

const userReducerFn = (state = initialState, action) => {
  switch (action.type) {
    default:
      return state
  }
}

export const userReducer = attachNestedReducers(userReducerFn, {
  profile: profileReducer,
})

状态对象

{
    some: 'state',
    profile: { /* ... */ }
}

这是功能

export function attachNestedReducers(original, reducers) {
  const nestedReducerKeys = Object.keys(reducers)
  return function combination(state, action) {
    const nextState = original(state, action)
    let hasChanged = false
    const nestedState = {}
    for (let i = 0; i < nestedReducerKeys.length; i++) {
      const key = nestedReducerKeys[i]
      const reducer = reducers[key]
      const previousStateForKey = nextState[key]
      const nextStateForKey = reducer(previousStateForKey, action)
      nestedState[key] = nextStateForKey
      hasChanged = hasChanged || nextStateForKey !== previousStateForKey
    }
    return hasChanged ? Object.assign({}, nextState, nestedState) : nextState
  }
}
Andrew Luca answered 2020-01-14T12:03:06Z
translate from https://stackoverflow.com:/questions/36786244/nested-redux-reducers