generated from react18-tools/turborepo-template
-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
6a7e6aa
commit 66ddc6c
Showing
8 changed files
with
141 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
18 changes: 18 additions & 0 deletions
18
packages/shared/src/client/demo/with-selectors/counter-without-selectors.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
18
packages/shared/src/client/demo/with-selectors/counter.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from "./with-selectors"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
28
packages/shared/src/client/demo/with-selectors/user-data.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
16
packages/shared/src/client/demo/with-selectors/with-selectors.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> | ||
); | ||
} |