# Server side data persistence

There are various ways to persist data in the back-end, since you can use the Spreasheet in both headless and stateful way (using `useSpreasheetState` hook, or your own custom hook) .

## Persisting using useSpreadsheetState

Spreadsheet gives you granular callbacks for every state change triggered by the user. You can call a REST or websocket API to trigger an API call to the back-end

```tsx
import React, { useState } from "react";
import {
  CellData,
  EmbeddedChart,
  EmbeddedObject,
  Sheet,
  CanvasGrid,
  SpreadsheetProvider,
  TableView,
} from "@rowsncolumns/spreadsheet";
import {
  SheetData,
  useSpreadsheetState,
} from "@rowsncolumns/spreadsheet-state";
import { functions } from "@rowsncolumns/functions";

export const Editor = () => {
  const App = () => {
    const [sheets, onChangeSheets] = useState<Sheet[]>([]);
    const [sheetData, onChangeSheetData] = useState<SheetData<CellData>>({});
    const [scale, onChangeScale] = useState(1);
    const [charts, onChangeCharts] = useState<EmbeddedChart[]>([]);
    const [embeds, onChangeEmbeds] = useState<EmbeddedObject[]>([]);
    const [tables, onChangeTables] = useState<TableView[]>([]);

    const {
      onChange,
      onInsertRow,
    } = useSpreadsheetState({
      sheets,
      sheetData,
      tables,
      functions,
      onChangeSheets,
      onChangeSheetData,
      onChangeEmbeds,
      onChangeCharts,
      onChangeTables,
      initialColorMode: "light",
    });
    return (
      <CanvasGrid
        sheetId={1}
        rowCount={100}
        columnCount={100}
        onChange={(sheetId, cell, value) => {
          // Persit data to server
          fetch("/post", {
            method: "POST",
            body: JSON.stringify({ action: "SET", value, cell, sheetId }),
          });

          // Optimistically update local UI state
          onChange(sheetId, cell, value);
        }}
        onInsertRow={(sheetId, rowIndex) => {
          // Update server
          fetch("/post", {
            method: "POST",
            body: JSON.stringify({ action: "INSERT_ROW", sheetId, rowIndex }),
          });
          // Optimistic update
          onInsertRow(sheetId, rowIndex);
        }}
      />
    );
  };

  return (
    <SpreadsheetProvider>
      <App />
    </SpreadsheetProvider>
  );
};

```


---

# Agent Instructions: 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/data-persistence/server-side-data-persistence.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.
