API Generic
reset()

reset

The reset method allows you to reset the entire store to its initial state.

reset(notify: boolean = true): void

Parameters

OptionTypeRequiredDescription
notifybooleanNoWhether to trigger a re-render for all components subscribed to the store. Defaults to true.

Returns

void

Description

The reset method resets the entire store to its initial state, as defined when the store was created. If notify is true (default), it will trigger re-renders in all components that are subscribed to any part of the store.

Example

import { createStore } from '@devlab/store';
 
type StoreStates = {
  user: {
    name: string;
    age: number;
    preferences: {
      theme: 'light' | 'dark';
    };
  };
}
 
// Create a store
const store = createStore<StoreStates>({
  initialData: {
    user: {
      name: 'John Doe',
      age: 30,
      preferences: {
        theme: 'light'
      }
    }
  }
});
 
// Make some changes
store.set('user.name', 'Jane Doe');
store.set('user.age', 31);
store.set('user.preferences.theme', 'dark');
 
// Reset the store
store.reset();
 
// Usage in a React component
function UserProfile() {
  const userName = store.use('user.name');
  const age = store.use('user.age');
  const theme = store.use('user.preferences.theme');
 
  return (
    <div>
      <p>Name: {userName}</p>
      <p>Age: {age}</p>
      <p>Theme: {theme}</p>
      <button onClick={() => store.reset()}>
        Reset Store
      </button>
    </div>
  );
}

This example demonstrates how to use the reset method to restore the store to its initial state, undoing all changes made since the store was created.