A plugin that persists and syncs RGS store between tabs.
+Optional
options: PersistOptionsA plugin that persists and syncs a value between tabs.
+Extract coomon create hook logic to utils
-Extract coomon create hook logic to utils
+setter function to set the state.
-setter function to set the state.
+craete subscriber function to subscribe to the store.
-craete subscriber function to subscribe to the store.
+Initialize the named store when invoked for the first time.
+Use this hook similar to useState
hook.
+
Use this hook similar to useState
hook.
The difference is that you need to pass a
unique key - unique across the app to make
this state accessible to all client components.
Unique key to identify the store.
+Optional
value: ValueType<T>Initial value of the store.
+Optional
plugins: Plugin<T>[]Plugins to be applied to the store.
Do not initialize the store. Useful when you want to initialize the store later. Note that the setter function is not available until the store is initialized.
-Creates a store with plugins.
+Creates a hook similar to useRGS, but with plugins to be applied on first invocation.
+A hook that automatically initializes the store (if not already initialized) with the given plugins.
+Creates a store with plugins.
-Creates a hook similar to useRGS, but with plugins to be applied on first invocation.
-A hook that automatically initializes the store (if not already initialized) with the given plugins.
-Optional
value: UOptional
doNotInit: booleanI've developed fantastic libraries leveraging React18 features using Zustand, and they performed admirably. However, when attempting to import from specific folders for better tree-shaking, the libraries encountered issues. Each import resulted in a separate Zustand store being created, leading to increased package size.
+I've developed fantastic libraries leveraging React18 features using Zustand, and they performed admirably. However, when attempting to import from specific folders for better tree-shaking, the libraries encountered issues. Each import resulted in a separate Zustand store being created, leading to increased package size.
As a solution, I set out to create a lightweight, bare minimum store that facilitates shared state even when importing components from separate files, optimizing tree-shaking.
-The default export from r18gs
is deprecated. Please switch to using import { useRGS } from "r18gs"
instead. The default export will be removed in the next major release.
✅ Full TypeScript Support
+The default export from r18gs
is deprecated. Please switch to using import { useRGS } from "r18gs"
instead. The default export will be removed in the next major release.
✅ Full TypeScript Support
✅ Unleash the full power of React18 Server components
✅ Compatible with all build systems/tools/frameworks for React18
✅ Documented with Typedoc (Docs)
✅ Examples for Next.js, Vite, and Remix
-Utilize this hook similarly to the useState
hook. However, ensure to pass a unique key, unique across the app, to identify and make this state accessible to all client components.
Utilize this hook similarly to the useState
hook. However, ensure to pass a unique key, unique across the app, to identify and make this state accessible to all client components.
const [state, setState] = useRGS<number>("counter", 1);
@@ -18,7 +18,7 @@
-For detailed instructions, see Getting Started
Enhance the functionality of the store by leveraging either the create
function, withPlugins
function, or the useRGSWithPlugins
hook from r18gs/dist/with-plugins
, enabling features such as storing to local storage, among others.
Enhance the functionality of the store by leveraging either the create
function, withPlugins
function, or the useRGSWithPlugins
hook from r18gs/dist/with-plugins
, enabling features such as storing to local storage, among others.
// store.ts
import { create } from "r18gs/dist/with-plugins";
import { persist } from "r18gs/dist/plugins"; /** You can create your own plugin or import third-party plugins */
export const useMyPersistentCounterStore = create<number>("persistent-counter", 0, [persist()]);
@@ -29,12 +29,12 @@
-For detailed instructions, see Leveraging Plugins
See contributing.md
-Interested in hands-on courses for getting started with Turborepo? Check out React and Next.js with TypeScript and The Game of Chess with Next.js, React and TypeScript
+See contributing.md
+Interested in hands-on courses for getting started with Turborepo? Check out React and Next.js with TypeScript and The Game of Chess with Next.js, React and TypeScript
-This library is licensed under the MPL-2.0 open-source license.
+This library is licensed under the MPL-2.0 open-source license.
Please consider enrolling in our courses or sponsoring our work.
with 💖 by Mayank Kumar Chaudhari
with 💖 by Mayank Kumar Chaudhari
This is a hack to reduce lib size + readability + not encouraging direct access to globalThis
-This is a hack to reduce lib size + readability + not encouraging direct access to globalThis
+
Use this hook similar to
useState
hook. +- Preparing search index...
- The search index is not available
React18 Global StoreFunction useRGS
Use this hook similar to
-useState
hook. The difference is that you need to pass a unique key - unique across the app to make this state accessible to all client components.Example
Example
-Type Parameters
Parameters
Unique key to identify the store.
-Optional
value: ValueType<T>Initial value of the store.
-Returns [T, SetStateAction<T>]
+
Type Parameters
Parameters
Unique key to identify the store.
+Optional
value: ValueType<T>Initial value of the store.
+Returns [T, SetStateAction<T>]
Settings