Frontend State Management
Relevant source files
The following files were used as context for generating this wiki page:The react-grab project employs a multi-faceted approach to frontend state management. This includes the internal state of the core react-grab library, which is exposed via a public API, and the state management patterns used within the Next.js-based website application. The website primarily relies on standard React hooks (useState, useEffect, useCallback) for component-level state and context for sharing state across the component tree.
Architectural guidelines emphasize best practices such as synchronizing application state with the URL and using optimistic UI patterns for a better user experience. This document outlines the core library's state API, the patterns used in the website, and the guiding principles for state management across the project.
Sources: packages/react-grab/README.md, packages/website/AGENTS.md, packages/website/components/install-tabs.tsx
Core Library State API
The react-grab core library exposes a public API for initialization and state observation. This allows consuming applications to interact with and react to the library's internal state. The primary entry point is the init function.
Sources: packages/react-grab/README.md
Initialization and Configuration
The init function from react-grab/core accepts a configuration object that allows customization of behavior and, crucially, provides callbacks for state changes.
import { init } from "react-grab/core";
const api = init({
// ... theme options
onElementSelect: (element) => {
console.log("Selected:", element);
},
onCopySuccess: (elements, content) => {
console.log("Copied to clipboard:", content);
},
onStateChange: (state) => {
console.log("Active:", state.isActive);
},
});
Sources: packages/react-grab/README.md
The key state-related callbacks are summarized below:
| Callback | Description |
|---|---|
onElementSelect | Fired when a user selects an element. Receives the selected element object. |
onCopySuccess | Fired when context is successfully copied to the clipboard. |
onStateChange | Fired whenever the internal state of the library changes (e.g., activation). |
Sources: packages/react-grab/README.md
Public API Methods
The init function returns an API object with methods to control and inspect the library's state.
| Method | Description |
|---|---|
activate() | Programmatically activates the grabber UI. |
copyElement(element) | Programmatically copies a given DOM element's context. |
getState() | Returns the current state of the library. |
Sources: packages/react-grab/README.md
State Change Flow
The following diagram illustrates how a user interaction flows through the react-grab system and communicates state changes back to the host application.
This diagram shows the sequence of events and callbacks when a user interacts with react-grab.
Sources: packages/react-grab/README.md
Website Application State Management
The packages/website application, built with Next.js, demonstrates practical state management using React hooks and context.
Sources: packages/website/components/install-tabs.tsx, packages/website/app/blog/visual-edit/page.tsx
Component-Level State with Hooks
Standard React hooks are the primary tool for managing local component state. The InstallTabs component, for example, uses useState to manage the active tab, copy status, and mobile detection.
// packages/website/components/install-tabs.tsx
export const InstallTabs = ({ ... }) => {
const { customHotkey } = useHotkey();
const [activeTabId, setActiveTabId] = useState<string>(...);
const [didCopy, setDidCopy] = useState(false);
const [highlightedCodes, setHighlightedCodes] = useState<Record<string, string>>({});
const [isMobile, setIsMobile] = useState(false);
// ...
}
Sources: packages/website/components/install-tabs.tsx:199-206
Asynchronous Operations with useEffect
useEffect is used to handle side effects, such as fetching data or performing actions after a state change. In the HighlightedCodeBlock component, it is used to asynchronously highlight code whenever the code or lang props change.
// packages/website/app/blog/visual-edit/page.tsx
const HighlightedCodeBlock = ({ code, lang }: HighlightedCodeBlockProps) => {
const [highlightedHtml, setHighlightedHtml] = useState<string>("");
// ...
useEffect(() => {
const highlight = async () => {
const html = await highlightCode({ code, lang, showLineNumbers: false });
setHighlightedHtml(html);
};
highlight();
}, [code, lang]);
// ...
};
Sources: packages/website/app/blog/visual-edit/page.tsx:21-31
The flow for this component can be visualized as follows:
This flowchart shows the lifecycle of the HighlightedCodeBlock component's state.
Sources: packages/website/app/blog/visual-edit/page.tsx:21-31
Shared State with Context
The application uses React Context to share global or cross-component state. The InstallTabs component consumes a customHotkey value from useHotkey(), implying the existence of a HotkeyContext that provides this shared state to different parts of the application.
Sources: packages/website/components/install-tabs.tsx:199
URL as State
A key architectural principle is to use the URL to reflect application state, enabling deep linking. The project guidelines explicitly recommend this and mention the use of the nuqs library for this purpose. The packages/website/package.json confirms nuqs as a dependency.
Sources: packages/website/AGENTS.md:31, packages/website/package.json:20
State Management Principles
The AGENTS.md file contains a set of concise rules that guide development, including several that pertain directly to state management.
| Rule Category | Guideline |
|---|---|
| State & Navigation | MUST: URL reflects state (filters, tabs, pagination) to allow deep-linking. |
| State & Navigation | MUST: Back/Forward browser actions restore scroll position. |
| Feedback | SHOULD: Use Optimistic UI; reconcile on response, and show an error/rollback on failure. |
| Feedback | MUST: Confirm destructive actions or provide an "Undo" window. |
| Inputs & Forms | MUST: Warn on unsaved changes before navigation. |
| Inputs & Forms | MUST: Keep submit button enabled until the request starts, then disable and show a spinner. |
Sources: packages/website/AGENTS.md:16-43