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
4a5ac6e
commit c0533e1
Showing
7 changed files
with
136 additions
and
0 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
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> | ||
<h2>Counter Without Selectors</h2> | ||
<p>Rerender is triggered every time the state changes.</p> | ||
<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 data-testid="counter2-display">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,22 @@ | ||
import { useRef } from "react"; | ||
import { useStore } from "./store"; | ||
|
||
export function Counter() { | ||
const [{ count }, setState] = useStore(/^count$/); | ||
const renderCount = useRef(0); | ||
renderCount.current++; | ||
return ( | ||
<div> | ||
<h2>Counter With Selectors</h2> | ||
<p>Rerender is triggered by RGS only when count changes.</p> | ||
<p>Count: {count}</p> | ||
<button | ||
data-testid="increment-btn" | ||
onClick={() => setState(state => ({ ...state, count: count + 1 }))}> | ||
Increment | ||
</button> | ||
<button onClick={() => setState(state => ({ ...state, count: count - 1 }))}>Decrement</button> | ||
<p data-testid="counter1-display">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,24 @@ | ||
import { useRef } from "react"; | ||
import { useStore } from "./store"; | ||
import styles from "../demo.module.scss"; | ||
import { listToRegExp } from "../../src/utils"; | ||
|
||
export function Header() { | ||
const [{ name }] = useStore(listToRegExp(["name"])); | ||
const renderCount = useRef(0); | ||
renderCount.current++; | ||
return ( | ||
<> | ||
<h1>Example with Selectors</h1> | ||
<header className={styles.header}> | ||
<h2>My name is {name}</h2> | ||
<small> | ||
<i> | ||
Updates only when <code>name</code> changes.{" "} | ||
<code data-testid="header-render-count">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,25 @@ | ||
import { useRGS } from "../../src"; | ||
|
||
interface MyStore { | ||
count: number; | ||
name: string; | ||
user: { | ||
name: string; | ||
age: number; | ||
}; | ||
} | ||
|
||
export const useStore = (includeRegExp?: RegExp | null | 0, excludeRegExp?: RegExp) => | ||
useRGS<MyStore>( | ||
"my-store-with-selectors", | ||
{ | ||
count: 0, | ||
name: "John", | ||
user: { | ||
name: "John", | ||
age: 30, | ||
}, | ||
}, | ||
includeRegExp, | ||
excludeRegExp, | ||
); |
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> | ||
); | ||
} |
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 { 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 /> | ||
<div className={styles.flex}> | ||
<Counter /> | ||
<CounterWithoutSelectors /> | ||
<UserData /> | ||
</div> | ||
</div> | ||
); | ||
} |