From 66ddc6c04a4dc62b58bf150c2fcb5a3d35ff48ea Mon Sep 17 00:00:00 2001 From: Mayank Date: Tue, 17 Dec 2024 19:59:06 +0530 Subject: [PATCH] demo with issues --- packages/shared/src/client/demo/demo.tsx | 20 +++++++++++-- .../counter-without-selectors.tsx | 18 ++++++++++++ .../client/demo/with-selectors/counter.tsx | 18 ++++++++++++ .../src/client/demo/with-selectors/header.tsx | 19 +++++++++++++ .../src/client/demo/with-selectors/index.ts | 1 + .../src/client/demo/with-selectors/store.ts | 24 ++++++++++++++++ .../client/demo/with-selectors/user-data.tsx | 28 +++++++++++++++++++ .../demo/with-selectors/with-selectors.tsx | 16 +++++++++++ 8 files changed, 141 insertions(+), 3 deletions(-) create mode 100644 packages/shared/src/client/demo/with-selectors/counter-without-selectors.tsx create mode 100644 packages/shared/src/client/demo/with-selectors/counter.tsx create mode 100644 packages/shared/src/client/demo/with-selectors/header.tsx create mode 100644 packages/shared/src/client/demo/with-selectors/index.ts create mode 100644 packages/shared/src/client/demo/with-selectors/store.ts create mode 100644 packages/shared/src/client/demo/with-selectors/user-data.tsx create mode 100644 packages/shared/src/client/demo/with-selectors/with-selectors.tsx diff --git a/packages/shared/src/client/demo/demo.tsx b/packages/shared/src/client/demo/demo.tsx index 67f1f0c8..c3b64be4 100644 --- a/packages/shared/src/client/demo/demo.tsx +++ b/packages/shared/src/client/demo/demo.tsx @@ -5,10 +5,20 @@ import styles from "./demo.module.scss"; import basicExampleCode from "./basic-example/basic-example.tsx?raw"; import counterCode from "./basic-example/counter.tsx?raw"; import { CodeDisplay } from "./code-display"; +import { WithSelector } from "./with-selectors"; +import withSelectorCode from "./with-selectors/with-selectors.tsx?raw"; +import storeCode from "./with-selectors/store.ts?raw"; +import couter2Code from "./with-selectors/counter.tsx?raw"; -const code = [ - { filename: "basic-example.tsx", code: basicExampleCode }, +const basicExCode = [ { filename: "counter.tsx", code: counterCode }, + { filename: "basic-example.tsx", code: basicExampleCode }, +]; + +const withSelectorExCode = [ + { filename: "store.ts", code: storeCode }, + { filename: "counter.tsx", code: couter2Code }, + { filename: "with-selectors.tsx", code: withSelectorCode }, ]; /** React live demo */ @@ -21,7 +31,11 @@ export function Demo() {
- + +
+
+ +
); diff --git a/packages/shared/src/client/demo/with-selectors/counter-without-selectors.tsx b/packages/shared/src/client/demo/with-selectors/counter-without-selectors.tsx new file mode 100644 index 00000000..526f86d3 --- /dev/null +++ b/packages/shared/src/client/demo/with-selectors/counter-without-selectors.tsx @@ -0,0 +1,18 @@ +import { useRef } from "react"; +import { useStore } from "./store"; + +export function CounterWithoutSelectors() { + const [{ count }, setState] = useStore(); + const renderCount = useRef(0); + renderCount.current++; + return ( +
+

Counter Without Selectors

+

Rerender is triggered every time the state changes.

+

Count: {count}

+ + +

Render Count: {renderCount.current}

+
+ ); +} diff --git a/packages/shared/src/client/demo/with-selectors/counter.tsx b/packages/shared/src/client/demo/with-selectors/counter.tsx new file mode 100644 index 00000000..204b7860 --- /dev/null +++ b/packages/shared/src/client/demo/with-selectors/counter.tsx @@ -0,0 +1,18 @@ +import { useRef } from "react"; +import { useStore } from "./store"; + +export function Counter() { + const [{ count }, setState] = useStore("count"); + const renderCount = useRef(0); + renderCount.current++; + return ( +
+

Counter With Selectors

+

Rerender is triggered by RGS only when count changes.

+

Count: {count}

+ + +

Render Count: {renderCount.current}

+
+ ); +} diff --git a/packages/shared/src/client/demo/with-selectors/header.tsx b/packages/shared/src/client/demo/with-selectors/header.tsx new file mode 100644 index 00000000..a6fe377a --- /dev/null +++ b/packages/shared/src/client/demo/with-selectors/header.tsx @@ -0,0 +1,19 @@ +import { useRef } from "react"; +import { useStore } from "./store"; + +export function Header() { + const [{ name }] = useStore("name"); + const renderCount = useRef(0); + renderCount.current++; + return ( +
+

My name is {name}

+ + + Updates only when name changes.{" "} + renderCount = {renderCount.current} + + +
+ ); +} diff --git a/packages/shared/src/client/demo/with-selectors/index.ts b/packages/shared/src/client/demo/with-selectors/index.ts new file mode 100644 index 00000000..7c01b7d4 --- /dev/null +++ b/packages/shared/src/client/demo/with-selectors/index.ts @@ -0,0 +1 @@ +export * from "./with-selectors"; diff --git a/packages/shared/src/client/demo/with-selectors/store.ts b/packages/shared/src/client/demo/with-selectors/store.ts new file mode 100644 index 00000000..19daa919 --- /dev/null +++ b/packages/shared/src/client/demo/with-selectors/store.ts @@ -0,0 +1,24 @@ +import { useRGS } from "r18gs"; + +interface MyStore { + count: number; + name: string; + user: { + name: string; + age: number; + }; +} + +export const useStore = (...selectors: (keyof MyStore)[]) => + useRGS( + "my-store-with-selectors", + { + count: 0, + name: "John", + user: { + name: "John", + age: 30, + }, + }, + ...selectors, + ); diff --git a/packages/shared/src/client/demo/with-selectors/user-data.tsx b/packages/shared/src/client/demo/with-selectors/user-data.tsx new file mode 100644 index 00000000..ed7fed5d --- /dev/null +++ b/packages/shared/src/client/demo/with-selectors/user-data.tsx @@ -0,0 +1,28 @@ +import { useRef } from "react"; +import { useStore } from "./store"; + +export function UserData() { + const [{ user }, setState] = useStore("user"); + const renderCount = useRef(0); + renderCount.current++; + const onSubmit = (e: React.FormEvent) => { + e.preventDefault(); + const formData = new FormData(e.currentTarget); + const name = formData.get("name") as string; + const age = Number(formData.get("age")); + setState(state => ({ ...state, user: { name, age }, name })); + }; + return ( +
+

UserData

+ renderCount = {renderCount.current} +

Name: {user.name}

+

Age: {user.age}

+
+ + + +
+
+ ); +} diff --git a/packages/shared/src/client/demo/with-selectors/with-selectors.tsx b/packages/shared/src/client/demo/with-selectors/with-selectors.tsx new file mode 100644 index 00000000..de438b03 --- /dev/null +++ b/packages/shared/src/client/demo/with-selectors/with-selectors.tsx @@ -0,0 +1,16 @@ +import { Counter } from "./counter"; +import styles from "../demo.module.scss"; +import { CounterWithoutSelectors } from "./counter-without-selectors"; +import { Header } from "./header"; +import { UserData } from "./user-data"; + +export function WithSelector() { + return ( +
+ {/*
*/} + {/* */} + + +
+ ); +}