🚀 useMutableState: Tiny state management hook to easily edit deeply nested properties

After years of frustration with React’s immutability rules—constantly spreading state just to update a nested property—I decided enough was enough. The code was cluttered, error-prone, and hard to read.

So, I built useMutableState(), a tiny React hook…


This content originally appeared on DEV Community and was authored by Ragaeeb

After years of frustration with React’s immutability rules—constantly spreading state just to update a nested property—I decided enough was enough. The code was cluttered, error-prone, and hard to read.

So, I built useMutableState(), a tiny React hook that leverages the Proxy pattern to make deep state updates effortless. No more spreading—just mutate directly, and React handles the rest!

Turn:

const [state, setState] = useState({
    level1: { level2: { level3: { level4: { count: 0 } } } },
});

const increment = () => {
    setState((prev) => ({
        ...prev,
        level1: {
            ...prev.level1,
            level2: {
                ...prev.level1.level2,
                level3: {
                    ...prev.level1.level2.level3,
                    level4: {
                        ...prev.level1.level2.level3.level4,
                        count: prev.level1.level2.level3.level4.count + 1,
                    },
                },
            },
        },
    }));
};

return (
    <div>
        <h3>Count: {state.level1.level2.level3.level4.count}</h3>
        <button onClick={increment}>Increment</button>
    </div>
);

into:

    const state = useMutableState({
        level1: { level2: { level3: { level4: { count: 0 } } } },
    });

    return (
        <div>
            <h3>Count: {state.level1.level2.level3.level4.count}</h3>
            <button onClick={() => state.level1.level2.level3.level4.count++}>Increment</button>
        </div>
    );

đź”— See it in action: Demo
đź’» Check out the code: Repo

Bug reports are welcome! 🚀

Image description

Image description


This content originally appeared on DEV Community and was authored by Ragaeeb


Print Share Comment Cite Upload Translate Updates
APA

Ragaeeb | Sciencx (2025-02-18T21:59:54+00:00) 🚀 useMutableState: Tiny state management hook to easily edit deeply nested properties. Retrieved from https://www.scien.cx/2025/02/18/%f0%9f%9a%80-usemutablestate-tiny-state-management-hook-to-easily-edit-deeply-nested-properties/

MLA
" » 🚀 useMutableState: Tiny state management hook to easily edit deeply nested properties." Ragaeeb | Sciencx - Tuesday February 18, 2025, https://www.scien.cx/2025/02/18/%f0%9f%9a%80-usemutablestate-tiny-state-management-hook-to-easily-edit-deeply-nested-properties/
HARVARD
Ragaeeb | Sciencx Tuesday February 18, 2025 » 🚀 useMutableState: Tiny state management hook to easily edit deeply nested properties., viewed ,<https://www.scien.cx/2025/02/18/%f0%9f%9a%80-usemutablestate-tiny-state-management-hook-to-easily-edit-deeply-nested-properties/>
VANCOUVER
Ragaeeb | Sciencx - » 🚀 useMutableState: Tiny state management hook to easily edit deeply nested properties. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2025/02/18/%f0%9f%9a%80-usemutablestate-tiny-state-management-hook-to-easily-edit-deeply-nested-properties/
CHICAGO
" » 🚀 useMutableState: Tiny state management hook to easily edit deeply nested properties." Ragaeeb | Sciencx - Accessed . https://www.scien.cx/2025/02/18/%f0%9f%9a%80-usemutablestate-tiny-state-management-hook-to-easily-edit-deeply-nested-properties/
IEEE
" » 🚀 useMutableState: Tiny state management hook to easily edit deeply nested properties." Ragaeeb | Sciencx [Online]. Available: https://www.scien.cx/2025/02/18/%f0%9f%9a%80-usemutablestate-tiny-state-management-hook-to-easily-edit-deeply-nested-properties/. [Accessed: ]
rf:citation
» 🚀 useMutableState: Tiny state management hook to easily edit deeply nested properties | Ragaeeb | Sciencx | https://www.scien.cx/2025/02/18/%f0%9f%9a%80-usemutablestate-tiny-state-management-hook-to-easily-edit-deeply-nested-properties/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.