create service of a state manager that contain any data with methods to operate with state in javascript

index.tsx
class StateManager {
    constructor(initialState) {
        this.state = initialState;
    }
    
    // Method to get the current state
    getState() {
        return this.state;
    }
    
    // Method to update the state
    setState(newState) {
        this.state = newState;
    }
    
    // Method to reset the state to initial state
    resetState() {
        this.state = initialState;
    }
    
    // You can add more methods based on your requirements
}

// Sample usage
const initialState = { count: 0 };
const stateManager = new StateManager(initialState);
console.log(stateManager.getState()); // Output: { count: 0 }

stateManager.setState({ count: 1 });
console.log(stateManager.getState()); // Output: { count: 1 }

stateManager.resetState();
console.log(stateManager.getState()); // Output: { count: 0 }
827 chars
34 lines

gistlibby LogSnag