npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2025 – Pkg Stats / Ryan Hefner

@rustling-pines/ag-grid-react-state-manager

v0.0.4

Published

Utility that captures AG Grid state changes, such as column reordering, sorting, and filtering, and converts them into a JSON format for persistence and restoration.

Readme

AgGridReact StateManager

A lightweight utility that captures AG Grid state changes, such as column reordering, sorting, and filtering, and converts them into a JSON format for seamless state persistence and restoration in React applications.

This package supports both single-grid and multi-grid scenarios, providing hooks that simplify AG Grid state management and enable integration with local storage, APIs, or centralized state stores.

Licensing Notice

This package interacts with AG Grid, which may require a separate commercial license for its Enterprise features. Ensure you comply with AG Grid’s license terms. This package does not include or distribute any AG Grid license.

License

This project is licensed under the MIT License - see the LICENSE file for details.

Installation

npm install @rustling-pines/ag-grid-react-state-manager

USAGE

Component with a Single Grid Scenario

import React, { useEffect } from 'react';
import { AgGridReact } from 'ag-grid-react';
import { useGridStateManager, IAgGridState } from '@rustling-pines/ag-grid-react-state-manager';

const SingleGridComponent = () => {

   const {
      gridKey,              // Unique identifier for the grid
      resetGridState,       // Reset the grid to its default state
      clearGridState,       // Clear all saved grid states
      
      // Get the current grid state as a JSON object;
      // use this to save to an API or localStorage based on your needs
      currentGridState,

      // Set the grid's state programmatically from an API or localStorage
      setGridState,

      // Get the necessary props to manage grid state
      getStateManagementProps,

   } = useGridStateManager({

      // Unique identifier for the grid
      gridKey: 'grid-1', 

      // Optional! Load state from localStorage or API
      defaultGridState: JSON.parse(localStorage.getItem('grid-1') || '{}'),

      // Optional (Method-1): Listen for state change events, ignore this if observing currentState
      onStateChange: (gridKey, newState) => { 
         // Persist updated state to localStorage
         localStorage.setItem(gridKey, JSON.stringify(newState));
         console.log(`Grid [${gridKey}] state changed:`, newState);
      },

   });

   // Optional (Method-2): observe the currentGridState directly (no need to rely solely on onStateChange)
   useEffect(() => {
      localStorage.setItem(gridKey, JSON.stringify(currentGridState));
   }, [gridKey, currentGridState]);

   useEffect(() => {
      // Ensure grid state is restored on component mount from localStorage or API
      const savedState = localStorage.getItem(gridKey);
      if (savedState) {
         setGridState(JSON.parse(savedState) as IAgGridState);
      }
   }, [gridKey, setGridState]);

   return (
      <div>
         <button onClick={handleSaveToApi}>Save to Api</button>
         <button onClick={resetGridState}>Reset Grid State</button>
         <button onClick={clearGridState}>Clear Grid State</button>
         <div className="ag-theme-alpine" style={{ width: '100%', height: '400px' }}>
            <AgGridReact
               columnDefs={[
                  { field: 'name', sortable: true, filter: true },
                  { field: 'age', sortable: true, filter: true },
               ]}
               rowData={[
                  { name: 'John', age: 25 },
                  { name: 'Jane', age: 30 },
                  { name: 'Alice', age: 28 },
               ]}
               {...getStateManagementProps()}
            />
         </div>
      </div>
   );
};

export default SingleGridComponent;

Component with Multiple Grids

import React from 'react';
import { AgGridReact } from 'ag-grid-react';
import { useMultiGridStateManager } from '@rustling-pines/ag-grid-react-state-manager';

const MultiGridComponent = () => {

   const { gridStates, createGridManager } = useMultiGridStateManager();

   const grid1Manager = createGridManager({
      gridKey: 'grid-1',
      defaultGridState: JSON.parse(localStorage.getItem('grid-1') || '{}'), // Restore from localStorage
      onStateChange: (key, newState) => {
         // Save the updated state to localStorage
         localStorage.setItem(key, JSON.stringify(newState));
         console.log(`Grid [${key}] state changed:`, newState);
      },
   });

   const grid2Manager = createGridManager({
      gridKey: 'grid-2',
      defaultGridState: JSON.parse(localStorage.getItem('grid-2') || '{}'), // Restore from localStorage
      onStateChange: (key, newState) => {
         // Save the updated state to localStorage
         localStorage.setItem(key, JSON.stringify(newState));
         console.log(`Grid [${key}] state changed:`, newState);
      },
   });

   return (
      <div>
         <div>
            <h3>Grid 1</h3>
            <button onClick={grid1Manager.resetGridState}>Reset Grid-1</button>
            <button onClick={grid1Manager.clearGridState}>Clear Grid-1</button>
            <div className="ag-theme-alpine" style={{ height: '300px', width: '100%' }}>
               <AgGridReact
                  columnDefs={[{ field: 'name' }, { field: 'age' }]}
                  rowData={[
                     { name: 'John', age: 25 },
                     { name: 'Jane', age: 30 }
                  ]}
                  {...grid1Manager.getStateManagementProps()}
               />
            </div>
         </div>

         <div>
            <h3>Grid 2</h3>
            <button onClick={grid2Manager.resetGridState}>Reset Grid-2</button>
            <button onClick={grid2Manager.clearGridState}>Clear Grid-2</button>
            <div className="ag-theme-alpine" style={{ height: '300px', width: '100%' }}>
               <AgGridReact
                  columnDefs={[{ field: 'product' }, { field: 'price' }]}
                  rowData={[
                     { product: 'Apple', price: 1.2 },
                     { product: 'Banana', price: 0.8 },
                  ]}
                  {...grid2Manager.getStateManagementProps()}
               />
            </div>
         </div>

         <h3>All Grid States:</h3>
         <pre>{JSON.stringify(gridStates, null, 2)}</pre>
      </div>
   );
};

export default MultiGridComponent;