Redux Reducer功能处于空状态

时间:2018-11-21 15:04:32

标签: react-redux redux-thunk

这对我来说有点奇怪,因为我的状态数组在同一个reducer的函数中为空,而在其他reducer的函数中则为空。

所以,我的减速器是这样的-

export function booksHasErrored(state = false, action) {
     //console.log(action.type);
    switch (action.type) {
        case 'BOOKS_HAS_ERRORED':
            return action.hasErrored;
        default:
            return state;
    }
}

export function booksIsLoading(state = false, action) {
    switch (action.type) {
        case 'BOOKS_IS_LOADING':
            return action.isLoading;
        default:
            return state;
    }
}

export function books(state = [], action) {
    switch (action.type) {
        case 'BOOKS_FETCH_DATA_SUCCESS':
            return action.books;
        default:
            return state;
    }
}

export function updateBook(state = [], action) {
    console.log('updateBook');
    console.log(state);
    console.log(action);
    switch (action.type) {
        case 'BOOK_UPDATE':
            return state;
        default:
            return state;
    }
}

在这个化简器中,功能书籍的状态已包含我所有书籍的列表。但是,updateBook函数始终显示空状态数组。在我的Web应用程序中,首先调用book函数从API调用中获取我所有图书的列表,然后调用update函数,并且用户导航以更新图书。我希望我的updateBook函数可以接收带有所有项目的状态对象。

这就是我组合减速器的方式-

import {combineReducers} from 'redux';
import {routerReducer} from 'react-router-redux';
import { books, booksHasErrored, booksIsLoading,updateBook } from './books';
import { search, searchHasErrored, searchIsLoading } from './search';
import { authors, authorsHasErrored, authorsIsLoading } from './authors';

//import authors from './authors';
//console.log(books);
const rootReducer = combineReducers({ books,booksHasErrored,booksIsLoading,updateBook,authors,search, searchHasErrored, searchIsLoading, authors, authorsHasErrored, authorsIsLoading,routing:routerReducer});

export default rootReducer;

如果您需要更多信息,请告诉我,我将在此处添加。

1 个答案:

答案 0 :(得分:0)

我可能已经发现您的问题,您未返回完整状态,您正在返回操作

export function books(state = [], action) {
    switch (action.type) {
        case 'BOOKS_FETCH_DATA_SUCCESS':
            return action.books;
        default:
            return state;
    }
}

您需要创建一个新状态并将其变异,然后返回它,例如:

export function books(state = [], action) {
    switch (action.type) {
        case 'BOOKS_FETCH_DATA_SUCCESS':
            return state.set('books', action.books);
        default:
            return state;
    }
}

您可以更改它并查看它是否有效吗?