utkarsh-1602 / ecommerce-admin

This repository provides a comprehensive solution for managing billboards, categories, and products, allowing you to create a customized online shopping experience similar to Shopify. With intuitive features, you can effortlessly design and organize your store as well.
https://ecommerce-admin-mauve-psi.vercel.app
MIT License
2 stars 0 forks source link

Why I've Used Zustand instead of Redux or any other State Management Library ? #58

Open utkarsh-1602 opened 10 months ago

utkarsh-1602 commented 10 months ago

In my hooks/use-store-modal.tsx, I've defined the zustand object

import { create } from "zustand";

// Defining an interface for the state of the modal store
interface useStoreModalStore {
    isOpen: boolean; // Indicates whether the modal is open or closed
    onOpen: () => void;   // Function to open the modal
    onClose: () => void; // Function to close the modal
}

// Creating a Zustand store for managing the modal state
export const UseStoreModal = create<useStoreModalStore>((set) => ({
    isOpen: false, // Initial state: modal is closed
    onOpen: () => set({ isOpen: true }), // Function to open the modal by setting `isOpen` to `true`
    onClose: () => set({ isOpen: false }) // Function to close the modal by setting `isOpen` to `false`
}))
utkarsh-1602 commented 10 months ago

Redux enforces immutability, which is a good practice for predictable state changes. However, it often requires a lot of boilerplate code to achieve this. Zustand simplifies immutability by allowing you to directly mutate state, thanks to its integration with React's useReducer and useState hooks.