Skip to content

Commit

Permalink
Update Readme and docs
Browse files Browse the repository at this point in the history
  • Loading branch information
mayank1513 committed Jun 22, 2024
1 parent 2eb306a commit 44a6354
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 25 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ I've developed fantastic libraries leveraging React18 features using Zustand, an

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.

### Important Announcement

The default export from `r18gs` is [deprecated](https://github.com/react18-tools/react18-global-store/discussions/31). Please switch to using `import { useRGS } from "r18gs"` instead. The default export will be removed in the next major release.

## Features

✅ Full TypeScript Support
Expand Down
42 changes: 21 additions & 21 deletions lib/tests/use-rgs.test.tsx
Original file line number Diff line number Diff line change
@@ -1,37 +1,37 @@
import { describe, test } from "vitest";
import { act, fireEvent, render, screen } from "@testing-library/react";
import useRGS from "../src";
import { useRGS } from "../src";
import { ChangeEvent, useCallback } from "react";

const COUNT_RGS_KEY = "count";
const TESTID_INPUT = "in1";
const TESTID_DISPLAY = "d1";

function Component1() {
const [count, setCount] = useRGS<number>(COUNT_RGS_KEY, 0);
const handleChange = useCallback(
(e: ChangeEvent<HTMLInputElement>) => {
setCount(parseInt(e.target.value));
},
[setCount],
);
return (
<div>
<input data-testid={TESTID_INPUT} onChange={handleChange} type="number" value={count} />
</div>
);
const [count, setCount] = useRGS<number>(COUNT_RGS_KEY, 0);
const handleChange = useCallback(
(e: ChangeEvent<HTMLInputElement>) => {
setCount(parseInt(e.target.value));
},
[setCount],
);
return (
<div>
<input data-testid={TESTID_INPUT} onChange={handleChange} type="number" value={count} />
</div>
);
}

function Component2() {
const [count] = useRGS<number>(COUNT_RGS_KEY);
return <h1 data-testid={TESTID_DISPLAY}>{count}</h1>;
const [count] = useRGS<number>(COUNT_RGS_KEY);
return <h1 data-testid={TESTID_DISPLAY}>{count}</h1>;
}

describe("React18GlobalStore", () => {
test("check state update to multiple components", async ({ expect }) => {
render(<Component1 />);
render(<Component2 />);
await act(() => fireEvent.input(screen.getByTestId(TESTID_INPUT), { target: { value: 5 } }));
expect(screen.getByTestId(TESTID_DISPLAY).textContent).toBe("5");
});
test("check state update to multiple components", async ({ expect }) => {
render(<Component1 />);
render(<Component2 />);
await act(() => fireEvent.input(screen.getByTestId(TESTID_INPUT), { target: { value: 5 } }));
expect(screen.getByTestId(TESTID_DISPLAY).textContent).toBe("5");
});
});
4 changes: 2 additions & 2 deletions md-docs/1.getting-started.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ export const COUNTER = "counter";
// components/display.tsx
"use client";

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

export default function Display() {
Expand All @@ -73,7 +73,7 @@ export default function Display() {
// components/counter.tsx
"use client";

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

export default function Counter() {
Expand Down
2 changes: 1 addition & 1 deletion packages/shared/src/client/demo/counter.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import useRGS from "r18gs";
import { useRGS } from "r18gs";

const RGS_COUNTER_KEY = "counter";

Expand Down
2 changes: 1 addition & 1 deletion packages/shared/src/client/demo/demo.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { CounterController, CounterDisplay } from "./counter";
import styles from "./demo.module.scss";
import { Editor } from "react-live";

const code = `import useRGS from "r18gs";
const code = `import { useRGS } from "r18gs";
const MY_KEY = "counter";
Expand Down

0 comments on commit 44a6354

Please sign in to comment.