Install pinia-react with your preferred package manager:
This example uses the browser entry point of an existing React project, with an element whose id is app in the HTML. Call createPinia() once before rendering the application to initialize the state container used by Stores. For Next.js App Router, use the root Provider setup in the Next.js guide.
A Store groups shared state, derived values (Getters), and methods that change state (Actions). Define a counter in src/stores/counter.ts:
defineStore('counter', ...) generates useCounterStore and getCounterStore. Use the Hook in components and the plain getCounterStore() function outside them. See using Stores outside components.
In src/App.tsx, read count and change it through an Action:
Start your project's development server and click “Increment”; the count should change from 0 to 1. Public Store state is read-only: do not call counter.count++ directly in a component.
For a JSX project, use the same Store and component code in .js and .jsx files.
Continue with State, Getters, and Actions for type inference, derived values, and asynchronous changes.
For independently mounted roots, requests and final teardown, see instance ownership and disposal. disposePinia is an explicit, irreversible owner operation, not an Effect cleanup; Provider unmount does not dispose automatically.