首页 > 解决方案 > Redu-Saga 的 put 方法没有启动对应的 Reducer

问题描述

这是我的传奇(索引和特定传奇):传奇索引:


    import { all } from 'redux-saga/effects';

    import search from './search';

    export default function* rootSaga() {
        yield all([search]);
    }


具体传奇:


    import { put, call, takeLatest } from 'redux-saga/effects';
    import {
        SEARCH_MOVIE_START,
        SEARCH_MOVIE_COMPLETE,
        SEARCH_MOVIE_ERROR,
    } from '../../consts/actionTypes';

    import { apiCall } from '../api';

    export function* searchMovie({ playload }) {
        try {
            console.log('Accion inical');
            const results = yield call(
                apiCall,
                `&s=${playload.movieName}`,
                null,
                null,
                'GET'
            );
            yield put({ type: SEARCH_MOVIE_COMPLETE, results });
        } catch (error) {
            yield put({ type: SEARCH_MOVIE_ERROR, error });
        }
    }

    export default function* search() {
        yield takeLatest(SEARCH_MOVIE_START, searchMovie);
    }

正如你所看到的,在这个传奇中是一个 console.log() 和一个 put() 方法,但是 console.log() 和 put() 都不起作用,我的意思是在浏览器控制台中没有出现任何消​​息器。我也使用 Redux DevTools 并且在操作部分只出现 SERACH_MOVIE_START,但没有出现 SEARCH_MOVIE_COMPLETE 或 SEARCH_MOVIE_ERROR(如果我在我的 api 中调用失败)

在此处输入图像描述

我的减速器(索引和特定减速器): 我的索引:


    import { combineReducers } from 'redux';
    import search from './search';

    const rootReducer = combineReducers({
        search,
    });

    export default rootReducer;

我的专用减速机


    import {
        SEARCH_MOVIE_COMPLETE,
        SEARCH_MOVIE_ERROR,
        SEARCH_MOVIE_START,
    } from '../../consts/actionTypes';

    const initialState = {};

    export default function (state = initialState, action) {
        switch (action.type) {
            case SEARCH_MOVIE_START:
                return { ...state };
            case SEARCH_MOVIE_ERROR:
                console.log(action);
                return { ...state };
            case SEARCH_MOVIE_COMPLETE:
                console.log(action);
                return { si: 'no lo creo', ...state };
            default:
                return { ...state };
        }
    }

并存储:


    import { createStore, applyMiddleware } from 'redux';
    import createSagaMiddleware from 'redux-saga';
    import { composeWithDevTools } from 'redux-devtools-extension';

    import rootReducer from '../reducers';
    import rootSaga from '../sagas';

    const configureStore = () => {
        const sagaMiddleware = createSagaMiddleware();
        return {
            ...createStore(
                rootReducer,
                composeWithDevTools(applyMiddleware(sagaMiddleware))
            ),
            runSaga: sagaMiddleware.run(rootSaga),
        };
    };

    export default configureStore;

标签: javascriptreduxredux-saga

解决方案


推荐阅读