Search code examples
reduxredux-thunkredux-devtoolsredux-devtools-extension

how to apply redux developer tools with reduxThunk


here is my code

import React        from "react";
import ReactDOM     from "react-dom";
import { Provider } from 'react-redux';
import { createStore, applyMiddleware } from 'redux';
import "normalize.css/normalize.css"
import  "./styles/styles.scss";
import { Router, Route, IndexRoute, browserHistory } from 'react-router';
import reduxThunk from 'redux-thunk';
import { composeWithDevTools } from 'redux-devtools-extension';

import AppRouter from './routers/AppRouter';
import reducers from './reducers';
import {AUTH_USER} from "./actions/types";

//
const createStoreWithMiddleware =  applyMiddleware(reduxThunk)(createStore);
const store = createStoreWithMiddleware(reducers);


const token = localStorage.getItem('token');
if(token){
    store.dispatch({type:AUTH_USER});
}

ReactDOM.render(
    <Provider store={store}>
        <AppRouter />
    </Provider>
    , document.getElementById('app'));

so i want to Use redux-devtools-extension package from npm

how can i implement with createStoreWithMiddleware


Solution

  • Simply wrap the middleware with composeWithDevTools.Like at first import :

    import { composeWithDevTools } from 'redux-devtools-extension';
    

    Add all your middleware in an array.For now there is only one.

    const middleware = [
        reduxThunk,
    ];
    

    Then instead of createStoreWithMiddleWare do

    const store = createStore(reducers, composeWithDevTools(
      applyMiddleware(...middleware),
      // other store enhancers if any
    ));
    

    So the code becomes:

    import React        from "react";
    import ReactDOM     from "react-dom";
    import { Provider } from 'react-redux';
    import { createStore, applyMiddleware } from 'redux';
    import "normalize.css/normalize.css"
    import  "./styles/styles.scss";
    import { Router, Route, IndexRoute, browserHistory } from 'react-router';
    import reduxThunk from 'redux-thunk';
    import { composeWithDevTools } from 'redux-devtools-extension';
    
    import AppRouter from './routers/AppRouter';
    import reducers from './reducers';
    import {AUTH_USER} from "./actions/types";
    
    
    const middleware = [
        reduxThunk,
    ];
    
    const store = createStore(reducers, composeWithDevTools(
    applyMiddleware(...middleware),
    // other store enhancers if any
    ));
    
    
    const token = localStorage.getItem('token');
    if(token){
        store.dispatch({type:AUTH_USER});
    }
    
    ReactDOM.render(
        <Provider store={store}>
            <AppRouter />
        </Provider>
        , document.getElementById('app'));
    

    Haven't tested but should work.