Skip to content

Commit

Permalink
demo with issues
Browse files Browse the repository at this point in the history
  • Loading branch information
mayank1513 committed Dec 17, 2024
1 parent 6a7e6aa commit 66ddc6c
Show file tree
Hide file tree
Showing 8 changed files with 141 additions and 3 deletions.
20 changes: 17 additions & 3 deletions packages/shared/src/client/demo/demo.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand All @@ -21,7 +31,11 @@ export function Demo() {
</div>
<div className={styles.demo}>
<BasicExample />
<CodeDisplay code={code} />
<CodeDisplay code={basicExCode} />
</div>
<div className={styles.demo}>
<WithSelector />
<CodeDisplay code={withSelectorExCode} />
</div>
</>
);
Expand Down
Original file line number Diff line number Diff line change
@@ -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 (
<div>
<h1>Counter Without Selectors</h1>
<h2>Rerender is triggered every time the state changes.</h2>
<p>Count: {count}</p>
<button onClick={() => setState(state => ({ ...state, count: count + 1 }))}>Increment</button>
<button onClick={() => setState(state => ({ ...state, count: count - 1 }))}>Decrement</button>
<p>Render Count: {renderCount.current}</p>
</div>
);
}
18 changes: 18 additions & 0 deletions packages/shared/src/client/demo/with-selectors/counter.tsx
Original file line number Diff line number Diff line change
@@ -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 (
<div>
<h1>Counter With Selectors</h1>
<h2>Rerender is triggered by RGS only when count changes.</h2>
<p>Count: {count}</p>
<button onClick={() => setState(state => ({ ...state, count: count + 1 }))}>Increment</button>
<button onClick={() => setState(state => ({ ...state, count: count - 1 }))}>Decrement</button>
<p>Render Count: {renderCount.current}</p>
</div>
);
}
19 changes: 19 additions & 0 deletions packages/shared/src/client/demo/with-selectors/header.tsx
Original file line number Diff line number Diff line change
@@ -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 (
<header>
<h1>My name is {name}</h1>
<small>
<i>
Updates only when <code>name</code> changes.{" "}
<code>renderCount = {renderCount.current}</code>
</i>
</small>
</header>
);
}
1 change: 1 addition & 0 deletions packages/shared/src/client/demo/with-selectors/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from "./with-selectors";
24 changes: 24 additions & 0 deletions packages/shared/src/client/demo/with-selectors/store.ts
Original file line number Diff line number Diff line change
@@ -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<MyStore>(
"my-store-with-selectors",
{
count: 0,
name: "John",
user: {
name: "John",
age: 30,
},
},
...selectors,
);
28 changes: 28 additions & 0 deletions packages/shared/src/client/demo/with-selectors/user-data.tsx
Original file line number Diff line number Diff line change
@@ -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<HTMLFormElement>) => {
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 (
<div>
<h1>UserData</h1>
<small>renderCount = {renderCount.current}</small>
<p>Name: {user.name}</p>
<p>Age: {user.age}</p>
<form onSubmit={onSubmit}>
<input type="text" name="name" defaultValue={user.name} />
<input type="number" name="age" defaultValue={user.age} />
<button type="submit">Update User</button>
</form>
</div>
);
}
16 changes: 16 additions & 0 deletions packages/shared/src/client/demo/with-selectors/with-selectors.tsx
Original file line number Diff line number Diff line change
@@ -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 (
<div className={styles.preview}>
{/* <Header /> */}
{/* <Counter /> */}
<CounterWithoutSelectors />
<UserData />
</div>
);
}

0 comments on commit 66ddc6c

Please sign in to comment.