2017-06-19 121 views
0

我有bookManageReducer.jsx嵌套`combineReducers`不讓有狀態,而不嵌套對象

import { combineReducers } from 'redux' 
import { 
    REQUEST_BOOKS_PAGE, 
    RECEIVE_BOOKS_PAGE 
} from '../constants/dashboardConstants' 

const books = (state = [], action) => { 
    switch (action.type) { 
    case RECEIVE_BOOKS_PAGE: 
     return action.books 
    default: 
     return state 
    } 
} 

const booksState = (state = {isFetching: false}, action) => { 
    switch (action.type) { 
    case REQUEST_BOOKS_PAGE: 
     return {isFetching: true} 
    case RECEIVE_BOOKS_PAGE: 
     return {isFetching: true} 
    default: 
     return state 
    } 
} 
// ... 
const booksManageReducer = combineReducers({ books, employees, employeesList, booksPagination, booksState }) 

export default booksManageReducer 

我想是所有中間減速根減速dashboardReducer.jsx結合:

import { combineReducers } from 'redux' 
import { routerReducer } from 'react-router-redux' 
import booksManageReducer from './booksManageReducer' 

const companyId = (state = {}, action) => { 
    return state 
} 

const dashboardReducer = combineReducers({booksManageReducer, companyId, routing: routerReducer}) 

export default dashboardReducer 

哪產生這種狀態:

Object {booksManageReducer: Object, companyId: 1, routing: Object} 

而不是

Object {books: [], ..., companyId: 1, routing: Object} 

當我嘗試使用對象蔓延運營商:

常量dashboardReducer = combineReducers({... booksManageReducer,companyId,路由:routerReducer})

它只是從國家消失, Object.assign也不起作用。

回答

2

請勿將它們合併爲booksMangeReducer。相反,只需將它們全部導出爲命名的出口,並立即將所有的減速:

export { 
    books, 
    employees, 
    employeesList, 
    booksPagination, 
    booksState 
}; 

然後它們全部導入:

import { 
    books, 
    employees, 
    employeesList, 
    booksPagination, 
    booksState 
} from './booksManageReducer' 

然後用combineReducers分別結合他們都:

combineReducers({ 
    books, 
    employees, 
    employeesList, 
    booksPagination, 
    booksState, 
    companyId, 
    routing: routerReducer 
}); 

combineReducers使用傳遞對象的鍵來構造狀態。這將給出所需的層次結構。

另一種方式是類似你這樣做,可以導出一個包含所有減速自己的目標是什麼,然後導入對象和傳播它:

export default { 
    //books, employees, etc. 
}; 

然後:

import reducers from './booksManageReducer'; 

最後:

combineReducers({ 
    ...reducers, 
    companyId, 
    routing: routerReducer 
});