A generic confirmation modal with Redux, Redux Toolkit, and TypeScript

A generic confirmation modal with Redux, Redux Toolkit, and TypeScript

A generic confirmation modal with Redux, Redux Toolkit, and TypeScript. We explore ways to create a generic confirmation modal using React, Redux, Redux Toolkit, and TypeScript.

We often need to allow our users to perform various delicate operations. They often can’t be undone, which might have dire consequences. Therefore, the good idea is to prompt the user for confirmation.

Across our application, the above case might happen frequently. Creating a separate confirmation modal for each of them is far from ideal. In this article, we explore various ways to tackle this issue using React, Redux with Redux Toolkit, and TypeScript.

We focus on managing the state, and for the user interface, we use  Material-UI.

A simple approach

The most straightforward approach worth noting is storing the callback of the confirm button in the store.

interface ConfirmationModalState {

   isOpened : boolean ;

   onConfirm : (() => void ) | null ;

}

const { open } = useConfirmationModalManagement ();

const { deleteCurrentPost } = usePostsManagement ();

const handleDeleteButtonClick = () => {

  open ({

     onConfirmation : deleteCurrentPost

   });

}

While it is possible, it is  officially discouraged by the Redux maintainers. The same goes for storing any other non-serializable items, such as promises. Also, one of the goals of Redux Toolkit is to help provide good defaults when working with a Redux store. Therefore, storing non-serializable values in our store results in Redux Toolkits showing warnings in the console. To get rid of them, we can use the  serializableCheck property  described in the documentation.

javascript react typescript

Bootstrap 5 Complete Course with Examples

Bootstrap 5 Tutorial - Bootstrap 5 Crash Course for Beginners

Nest.JS Tutorial for Beginners

Hello Vue 3: A First Look at Vue 3 and the Composition API

Building a simple Applications with Vue 3

Deno Crash Course: Explore Deno and Create a full REST API with Deno

How to Build a Real-time Chat App with Deno and WebSockets

Convert HTML to Markdown Online

HTML entity encoder decoder Online

How native is React Native? | React Native vs Native App Development

Article covers: How native is react native?, React Native vs (Ionic, Cordova), Similarities and difference between React Native and Native App Development.

React With Typescript, Redux And Thunk - React Javascript Tutorial

Wanted to make a video with redux and redux-thunk using typescript. I found this a bit confusing when I first learned it and hopefully this videos makes the ...

React cloneElement: A Better Way to Build a Component API Props in ES6 Javascript and Ts

Using the React cloneElement is a better way to build a component API props in ES6 Javascript and Typescript. Here's a tutorial on how to do so.

Make an App with React JS / JavaScript: React and TypeScript

Building a Web Application as a Front-End Developer using React JS and TypeScript! Learn how to use the “Thinking in React” method and apply it to your real web application. We will be using real live weather data to display in our application and make our components reusable and maintainable.

React With TypeScript, Redux And Thunk - React JavaScript Tutorial

Wanted to make a video with Redux and Redux-thunk using TypeScript. I found this a bit confusing when I first learned it and hopefully this videos makes the topic a bit easier to understand for you.