react-hook-deep-state
v1.1.4
Published
    to specify the property to update. - Flexible Updates: Supports both overriding and merging objects at any level of the state.
Installation
yarn add react-hook-deep-stateAPI
useDeepState<T>(initialState?: T)
Arguments:
initialState(optional): An initial state object of typeT. Defaults toundefinedif not provided.
Returns: A tuple [state, setDeepState]
state: The current state object of typeT.setDeepState: A function to update the state at a specific path.
setDeepState(value: StateValue<T, P, M>, path?: P, merge?: M)
Arguments:
value: The new value to set. The type ofvaluedepends on thepathandmergeparameters:- If
mergeistrue,valuecan be a deep partial object for merging. - If
mergeisfalse,valuemust match the exact type of the property at the specified path.
- If
path(optional): A string specifying the path to the property (e.g.,"nested.key"). If omitted or"", the entire state is updated.merge(optional): A boolean indicating whether to merge objects at the specified path. Defaults totrue.
Examples
Usage
import React from 'react';
import { useDeepState } from 'react-hook-deep-state';
const App = () => {
const [appState, setAppState] = useDeepState({
user: {
name: 'John Doe',
address: {
city: 'London',
zip: '76321',
country: 'GBR'
},
active: false,
hobbies: []
}
});
return (
<div>
<p>City: {appState.user.address.city}</p>
<p>Active: {appState.user.active ? 'Yes' : 'No'}</p>
<div>
<label htmlFor='name'>Set Name:</label>
<input
type='text'
id='name'
name='name'
value={appState.user.name}
onInput={(e) => setAppState(e.currentTarget.value, 'user.name')}
/>
</div>
</div>
);
};
export default App;Updating the Entire State
setAppState(
{
user: {
name: 'Jane Doe',
address: {
city: 'Berlin',
zip: '12345',
country: 'GER'
},
active: true,
hobbies: ['Cycling', 'Swimming']
}
},
'', // or undefined
true // Merge by default, set to false to override current value
);Update a Nested Property
setAppState('Tokyo', 'user.address.city');Merge a Nested Object
setAppState({ zip: '94101', country: 'JAP' }, 'user.address', true);Override a Nested Object
setAppState({ city: 'Canberra', zip: '90001', country: 'AUS' }, 'user.address', false);Update a Nested Array
setAppState(['Skydiving', 'Hiking'], 'user.hobbies');Running the Example Project
To see react-hook-deep-state in action, you can run the example project included in this repository.
Install Dependencies: Navigate to the root of the project and install the dependencies:
yarn installNavigate to the Example Folder: Move into the
examplefolder:cd exampleInstall Example Dependencies: Install the dependencies for the example project:
yarn installStart the Development Server: Run the example project:
yarn devView in Browser: Open your browser and navigate to
http://localhost:5173to interact with the example project.
License
This project is licensed under the MIT License.
Disclaimer: This project is not affiliated with the Illuminati, any government, or other "deep state" entities. It is purely a tool for managing deeply nested state in React applications.
