Skip to content

Commit

Permalink
Update REAMDE
Browse files Browse the repository at this point in the history
  • Loading branch information
mayank1513 committed Feb 22, 2024
1 parent 9508661 commit 3a68a18
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 2 deletions.
71 changes: 71 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,72 @@ or
$ yarn add r18gs
```

## Usage

Use this hook similar to `useState` hook.

The difference is that you need to pass an unique key - unique across the app to identify
and make this state accessible to all client components.

```tsx
const [state, setState] = useRGS<number>("counter", 1);
```

You can access the same state across all client side components using unique.

> It is recommended to store your keys in separate file to avoid typos and unnecessary conflicts.
### Example

```tsx
// constants/global-states.ts
export const COUNTER = "counter";
```

```tsx
// components/display.tsx
"use client";

import useRGS from "r18gs";
import { COUNTER } from "../constants/global-states";

export default function Display() {
const [count] = useRGS<number>(COUNTER);
return (
<div>
<h2>Client component 2</h2>
<b>{count}</b>
</div>
);
}
```

```tsx
// components/counter.tsx
"use client";

import useRGS from "r18gs";
import { COUNTER } from "../constants/global-states";

export default function Counter() {
const [count, setCount] = useRGS(COUNTER, 0);
return (
<div>
<h2>Clinet component 1</h2>
<input
onChange={e => {
setCount(parseInt(e.target.value.trim()));
}}
type="number"
value={count}
/>
</div>
);
}
```

## Contribute

### Build

To build all apps and packages, run the following command:
Expand All @@ -60,6 +126,11 @@ cd r18gs
pnpm dev
```

Also, please

1. check out discussion for providing any feedback or sugestions.
2. Report any issues or feature requests in issues tab

### 🤩 Don't forger to start [this repo](https://github.com/mayank1513/r18gs)!

Want hands-on course for getting started with Turborepo? Check out [React and Next.js with TypeScript](https://mayank-chaudhari.vercel.app/courses/react-and-next-js-with-typescript) and [The Game of Chess with Next.js, React and TypeScrypt](https://www.udemy.com/course/game-of-chess-with-nextjs-react-and-typescrypt/?referralCode=851A28F10B254A8523FE)
Expand Down
1 change: 1 addition & 0 deletions packages/shared-ui/src/constants/global-states.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const COUNTER = "counter";
3 changes: 2 additions & 1 deletion packages/shared-ui/src/root/counter.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
"use client";

import useRGS from "r18gs";
import { COUNTER } from "../constants/global-states";

export default function Counter() {
const [count, setCount] = useRGS("count", 0);
const [count, setCount] = useRGS(COUNTER, 0);
return (
<div>
<h2>Clinet component 1</h2>
Expand Down
3 changes: 2 additions & 1 deletion packages/shared-ui/src/root/display.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
"use client";

import useRGS from "r18gs";
import { COUNTER } from "../constants/global-states";

export default function Display() {
const [count] = useRGS<number>("count");
const [count] = useRGS<number>(COUNTER);
return (
<div>
<h2>Client component 2</h2>
Expand Down

0 comments on commit 3a68a18

Please sign in to comment.