What is Redux Toolkit and why it is more preferred?

While working as a Front-end Developer or Full Stack Developer, many engineers encountered Redux. But Recently Redux Team launched Redux Toolkit, an officially recommended and a SOPE library that stands for Simple, Opinionated, Powerful, and Effective state management library. It allows us to write more efficient code, speed up the development process, and automatically apply the best-recommended practices. It was mainly created to solve the THREE MAJOR ISSUES with Redux:

  • Configuring a Redux store is too complicated
  • Have to add a lot of packages to build a large scale application
  • Redux requires too much boilerplate code which makes it cumbersome to write efficient and clean code.

It also provides the automatic support for Redux Dev-tools Extension and for immer.js library which is a great tool to deal with immutable objects. You can also use the various predefined functions of Redux Toolkit which not only speeds up the process but also saves time.

Dependencies that comes along with Redux Toolkit:

  • immer
  • redux
  • redux-thunk
  • reselect

Install:

# NPM
npm install @reduxjs/toolkit

# Yarn
yarn add @reduxjs/toolkit

What Extra Features Are Provided:



  • A configureStore() function which provides automatic support for Redux-thunk, Redux DevTools Extension, and also passes the default middleware.
  • A createReducer() utility which provides support for immer library that allows writing the immutable code mutably.
  • A createAction() utility that returns an action creator function.
  • A createSlice() function that comes in handy to replace create action and create Reducer functions with a single function.
  • A createAsyncThunk() that takes Redux strings as arguments and returns a Promise.
  • A createEntityAdapter() utility that helps to perform CRUD operations.

A Basic Comparison Between Redux and Redux Toolkit(with React):

  1. Redux: Creating Store and reducers.

    Javascript

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    const addHandler=(state=0,action)=>{
    if(action.type==='ADD')
    {
        return state+1;
    }
    return state;
    }
      
    const store = createStore(addHandler);
      
    store.dispatch({type:'ADD'});

    chevron_right

    
    

  2. Redux Toolkit: Creating Store and reducers.

    Javascript

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // Action Creators
    const add = createAction('ADD'); 
      
    const addHandler = createReducer(0, {  
      [add]: state => state + 1
    })
      
    const store = configureStore({
      reducer: addHandler
    })
      
    store.dispatch(add());

    chevron_right

    
    

Using Redux DevTools Extension:

  1. React: You have to add a predefined statement while creating the store to access the Redux DevTools.

    Javascript



    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    const store = createStore(
      reducer,   window.__REDUX_DEVTOOLS_EXTENSION__ && 
                 window.__REDUX_DEVTOOLS_EXTENSION__()
    );

    chevron_right

    
    

  2. Redux Toolkit: It provides automatic support for Redux DevTools Extension.

    Javascript

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    const store = configureStore(
      reducer:rootReducer );

    chevron_right

    
    

Modifying The State:

  1. Redux: We need to manually handle and change the state immutably.

    Javascript

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    const initialState={
    counter:0}
      
    const handler=(state=initialState,action){
    return{
        ...state,
        counter:state.counter+1;
        }    
    }

    chevron_right

    
    

  2. Redux Toolkit: It provides the support for immer.js library which automatically changes the code immutably.

    Javascript

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    const initialState={
    counter:0}
      
    const handler=(state=initialState,action){
    return{
        counter:state.counter+1;
        }    
    }

    chevron_right

    
    

Conclusion:

  1. Redux Toolkit is beneficial to all Redux users regardless of skill level. It can be added as an internal part of the project at the start or can be added during the internal up-gradation in the existing one. 
  2. But it is still preferred that you should have the basic knowledge of how to handle and change the state immutably and also should have a glimpse of what is happening under the hood before migrating from Redux to Redux Toolkit.

react-js-img




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :

1


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.