> For the complete documentation index, see [llms.txt](https://docs.rowsncolumns.app/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.rowsncolumns.app/configuration/features/undo-redo.md).

# Undo/Redo

Change histories are stored as `immer` patches. But developers can choose their own state management modules, hence libraries like Redux or MobX can create state histories/patches.

`useSpreadsheetState` uses `immer` to product patches during state updates.

```tsx
import React, { useState } from "react";
import { CanvasGrid, CellData } from "@rowsncolumns/spreadsheet";
import { produceWithPatches } from "immer";
import {
  SheetData,
  useSpreadsheetState,
} from "@rowsncolumns/spreadsheet-state";

const MySpreadsheet = () => {
  const [sheetData, onChangeSheetData] = useState<SheetData<CellData>>({});
  const { createHistory } = useSpreadsheetState({});
  return (
    <CanvasGrid
      sheetId={1}
      rowCount={100}
      columnCount={100}
      onChange={(sheetId, activeCell, value) => {
        const saveHistory = createHistory();
        // Update sheet data
        onChangeSheetData?.((prevSheetData) => {
          const [nextState, patches, inversePatches] = produceWithPatches(
            prevSheetData,
            (draft) => {
              draft[sheetId][activeCell.rowIndex].values[
                activeCell.columnIndex
              ] = {
                ue: { sv: value },
              };
            }
          );

          saveHistory({
            sheetData: { patches, inversePatches },
          });

          return nextState;
        });
      }}
    />
  );
};
```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.rowsncolumns.app/configuration/features/undo-redo.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
