Redux toolkit | How to get started

Auraqule
3 min readSep 19, 2022

--

What is REDUX?

Redux is a predictable state management tool for JavaScript apps. It helps you write applications that behave consistently, run in different environments (client, server, and native), and are easy to test. On top of that, it provides a great developer experience, such as live code editing combined with a time traveling debugger.

You can use Redux together with React, or with any other view library. It is tiny (2kB, including dependencies), but has a large ecosystem of addons available.

The simplest way to pass data from a parent to a child in a React Application is by passing it on to the child’s props. But an issue arises when a deeply nested child requires data from a component higher up in the tree. If we pass on the data through the props, every single one of the children would be required to accept the data and pass it on to its child, leading to prop drilling, a terrible practice in the world of React. To solve the prop drilling issue in the world of REACT, we have State Management Solutions like Context API and Redux, but today we’ll focusing on REDUX.

According to REDUX documentation in 👉 https://redux.js.org

Redux Toolkit is our official recommended approach for writing Redux logic. It wraps around the Redux core, and contains packages and functions that we think are essential for building a Redux app. Redux Toolkit builds in our suggested best practices, simplifies most Redux tasks, prevents common mistakes, and makes it easier to write Redux applications.

How to use REDUX

The whole global state of your app is stored in an object tree inside a single STORE. The only way to change the state tree is to create an action, an object describing what happened, and dispatch it to the store. To specify how state gets updated in response to an action, you write pure reducer functions that calculate a new state based on the old state and the action.

Step 1- Create a REDUCER.

import { createSlice } "@reduxjs/toolkit";
export const slice = createSlice({ name: "slice-name", initialState: { }, reducers: { func01: (state) = > { }, } });
export const { func01 } = slice.actions;
export default slice.reducer;

Step 2- Configure the store.

import { configureStore } from "@reduxjs/toolkit"; 
import reducer from "./reducer";
export default configureStore({ reducer: { reducer: reducer } });

Step 3- Make the STORE available for data consumption.

import React from 'react';
import ReactDOM from 'react-dom';
import { Provider } from 'react-redux';
import App from './App.jsx'
import store from './store';
ReactDOM.render(
<Provider store={store}>
<App />
</Provider>,
document.getElementById("root")
);

Step 4- Use state or Dispatch Actions.

import { useSelector, useDispatch } from 'react-redux';
import { func01 } from './redux/reducer';
const Component = () => {
const reducerState = useSelector((state) => state.reducer);
const dispatch = useDispatch();
const doSomething = () = > dispatch(func01)
return (
<>
{/* ... */}
</>
);
}
export default Component;

Conclusion

Redux Toolkit is a great option for both beginners and developers who wants to reduce the amount of boilerplate code in Redux. It allows us to write cleaner and more readable code while keeping the Redux flow and pattern.

Thank you for reading. I hope this article has been helpful in getting you started with understanding Redux and using Redux Toolkit in your applications. Cheers! 😀

If you have any question or comments, feel free to drop it in the comment section below ✌

Originally published at https://auraqule.hashnode.dev.

--

--

Auraqule

Hi, I’m Christian Peters, a full-stack software developer, I love writing articles | content on my stack [“React js”, “Next js”, “Typescript”, “Node js”, … ]