Skip to content

Commit 66ddc6c

Browse files
committed
demo with issues
1 parent 6a7e6aa commit 66ddc6c

File tree

8 files changed

+141
-3
lines changed

8 files changed

+141
-3
lines changed

packages/shared/src/client/demo/demo.tsx

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,20 @@ import styles from "./demo.module.scss";
55
import basicExampleCode from "./basic-example/basic-example.tsx?raw";
66
import counterCode from "./basic-example/counter.tsx?raw";
77
import { CodeDisplay } from "./code-display";
8+
import { WithSelector } from "./with-selectors";
9+
import withSelectorCode from "./with-selectors/with-selectors.tsx?raw";
10+
import storeCode from "./with-selectors/store.ts?raw";
11+
import couter2Code from "./with-selectors/counter.tsx?raw";
812

9-
const code = [
10-
{ filename: "basic-example.tsx", code: basicExampleCode },
13+
const basicExCode = [
1114
{ filename: "counter.tsx", code: counterCode },
15+
{ filename: "basic-example.tsx", code: basicExampleCode },
16+
];
17+
18+
const withSelectorExCode = [
19+
{ filename: "store.ts", code: storeCode },
20+
{ filename: "counter.tsx", code: couter2Code },
21+
{ filename: "with-selectors.tsx", code: withSelectorCode },
1222
];
1323

1424
/** React live demo */
@@ -21,7 +31,11 @@ export function Demo() {
2131
</div>
2232
<div className={styles.demo}>
2333
<BasicExample />
24-
<CodeDisplay code={code} />
34+
<CodeDisplay code={basicExCode} />
35+
</div>
36+
<div className={styles.demo}>
37+
<WithSelector />
38+
<CodeDisplay code={withSelectorExCode} />
2539
</div>
2640
</>
2741
);
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import { useRef } from "react";
2+
import { useStore } from "./store";
3+
4+
export function CounterWithoutSelectors() {
5+
const [{ count }, setState] = useStore();
6+
const renderCount = useRef(0);
7+
renderCount.current++;
8+
return (
9+
<div>
10+
<h1>Counter Without Selectors</h1>
11+
<h2>Rerender is triggered every time the state changes.</h2>
12+
<p>Count: {count}</p>
13+
<button onClick={() => setState(state => ({ ...state, count: count + 1 }))}>Increment</button>
14+
<button onClick={() => setState(state => ({ ...state, count: count - 1 }))}>Decrement</button>
15+
<p>Render Count: {renderCount.current}</p>
16+
</div>
17+
);
18+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import { useRef } from "react";
2+
import { useStore } from "./store";
3+
4+
export function Counter() {
5+
const [{ count }, setState] = useStore("count");
6+
const renderCount = useRef(0);
7+
renderCount.current++;
8+
return (
9+
<div>
10+
<h1>Counter With Selectors</h1>
11+
<h2>Rerender is triggered by RGS only when count changes.</h2>
12+
<p>Count: {count}</p>
13+
<button onClick={() => setState(state => ({ ...state, count: count + 1 }))}>Increment</button>
14+
<button onClick={() => setState(state => ({ ...state, count: count - 1 }))}>Decrement</button>
15+
<p>Render Count: {renderCount.current}</p>
16+
</div>
17+
);
18+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import { useRef } from "react";
2+
import { useStore } from "./store";
3+
4+
export function Header() {
5+
const [{ name }] = useStore("name");
6+
const renderCount = useRef(0);
7+
renderCount.current++;
8+
return (
9+
<header>
10+
<h1>My name is {name}</h1>
11+
<small>
12+
<i>
13+
Updates only when <code>name</code> changes.{" "}
14+
<code>renderCount = {renderCount.current}</code>
15+
</i>
16+
</small>
17+
</header>
18+
);
19+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export * from "./with-selectors";
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import { useRGS } from "r18gs";
2+
3+
interface MyStore {
4+
count: number;
5+
name: string;
6+
user: {
7+
name: string;
8+
age: number;
9+
};
10+
}
11+
12+
export const useStore = (...selectors: (keyof MyStore)[]) =>
13+
useRGS<MyStore>(
14+
"my-store-with-selectors",
15+
{
16+
count: 0,
17+
name: "John",
18+
user: {
19+
name: "John",
20+
age: 30,
21+
},
22+
},
23+
...selectors,
24+
);
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import { useRef } from "react";
2+
import { useStore } from "./store";
3+
4+
export function UserData() {
5+
const [{ user }, setState] = useStore("user");
6+
const renderCount = useRef(0);
7+
renderCount.current++;
8+
const onSubmit = (e: React.FormEvent<HTMLFormElement>) => {
9+
e.preventDefault();
10+
const formData = new FormData(e.currentTarget);
11+
const name = formData.get("name") as string;
12+
const age = Number(formData.get("age"));
13+
setState(state => ({ ...state, user: { name, age }, name }));
14+
};
15+
return (
16+
<div>
17+
<h1>UserData</h1>
18+
<small>renderCount = {renderCount.current}</small>
19+
<p>Name: {user.name}</p>
20+
<p>Age: {user.age}</p>
21+
<form onSubmit={onSubmit}>
22+
<input type="text" name="name" defaultValue={user.name} />
23+
<input type="number" name="age" defaultValue={user.age} />
24+
<button type="submit">Update User</button>
25+
</form>
26+
</div>
27+
);
28+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import { Counter } from "./counter";
2+
import styles from "../demo.module.scss";
3+
import { CounterWithoutSelectors } from "./counter-without-selectors";
4+
import { Header } from "./header";
5+
import { UserData } from "./user-data";
6+
7+
export function WithSelector() {
8+
return (
9+
<div className={styles.preview}>
10+
{/* <Header /> */}
11+
{/* <Counter /> */}
12+
<CounterWithoutSelectors />
13+
<UserData />
14+
</div>
15+
);
16+
}

0 commit comments

Comments
 (0)