简体   繁体   中英

Redux reducer is not call after dispatch

Can you explain me why my code is not call my reducer (currently I have only one reducer in the folder) and doesn't activate redux-logger. My console is empty, there is no any information or errors

The main index.jsx

import React from "react";
import ReactDOM from "react-dom";
import { Provider } from 'react-redux';
import { createStore, combineReducers, applyMiddleware } from 'redux';
import logger from 'redux-logger';
import  App from './containers/App.jsx';
import * as reducers from './reducers'
import types from './constants/actions';

const reducer = combineReducers(reducers);

const destination = document.querySelector("#container");
const createStoreWithMiddleware = applyMiddleware(logger)(createStore);

let store = createStoreWithMiddleware(reducer, {
userName: 'N/A'
});


ReactDOM.render(
<Provider store={store}>
    <App/>
</Provider>,
destination
);

console.log(1) //<----- CONSOLE.LOG
store.dispatch({
           type: types.LOAD_USER_NAME,
           name : ''
 })

My reducer

import types from './../constants/actions'

const userName = (state = {userName : "N/A"}, action) => {
console.log('inside reducer'); //<----- CONSOLE.LOG
switch (action.type) {
    case types.LOAD_USER_NAME:
        console.log('before change state')
        return action.data;
    default:
        return '_N/A'
}
}

export {userName}

./constants/actions

module.exports = Object.freeze({
LOAD_USER_NAME : 'LOAD_USER_NAME'

})

afte execution I see in the console following information:

在此处输入图片说明

I hoped to see in the console at least one more time inside reducer which mean that my reducer was call.

The main problem in my code is apply middleware.

I substited this code:

const createStoreWithMiddleware = applyMiddleware(logger)(createStore);

let store = createStoreWithMiddleware(reducer, {
    userName: 'N/A'
});

On the following one:

const log = logger();

const store = createStore(
    reducer,
    {
      userName : 'N/A'
    },
    applyMiddleware(log)
);

and all start working

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM