Skip to content

Commit 775a8a7

Browse files
committed
wip: split into self and prop driven versions
1 parent 83f18cb commit 775a8a7

File tree

9 files changed

+101
-31
lines changed

9 files changed

+101
-31
lines changed
Loading

fast-context-generic-extended/src/App.tsx

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,10 @@ function App() {
1515
return (
1616
<AppFastContextProvider>
1717
<div className="container">
18-
<h1>App</h1>
18+
<img src="../public/DevToolsImage.png" style={{float: 'right', width: '350px'}} alt="Dev Tools"/>
19+
<h1>App (NO RE-RENDERS)</h1>
20+
<p>The App's Fast Context is created and exported here, but could be created in any file.</p>
21+
<p>Switch on the re-render highlight function (see image) in dev tools to see when re-renders happen.</p>
1922
<MainPage />
2023
</div>
2124
</AppFastContextProvider>

fast-context-generic-extended/src/app/createFastContext.tsx

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ export default function createFastContext<FastContext>(initialState: FastContext
1919
const subscribers = useRef(new Set<() => void>());
2020

2121
const set = useCallback((value: Partial<FastContext>) => {
22-
console.log('set', value);
2322
store.current = { ...store.current, ...value };
2423
subscribers.current.forEach((callback) => callback());
2524
}, []);
Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,24 @@
1-
import FormContainer from "./FormContainer";
2-
import DisplayContainer from "./DisplayContainer";
1+
import { PropDrivenDisplayContainer, SelfDrivenDisplayContainer } from "./DisplayContainer";
2+
import { PropDrivenFormContainer, SelfDrivenFormContainer } from "./FormContainer";
33

4-
export default function ContentContainer() {
5-
console.log(`Content Rendering`)
4+
export function PropDrivenContentContainer() {
5+
console.log(`Prop Driven Content Rendering`)
66
return (
77
<div className="container">
8-
<h3>ContentContainer</h3>
9-
<FormContainer />
10-
<DisplayContainer />
8+
<h3>'Prop Driven' Content Container (NO RE-RENDERS)</h3>
9+
<PropDrivenFormContainer />
10+
<PropDrivenDisplayContainer />
11+
</div>
12+
);
13+
};
14+
15+
export function SelfDrivenContentContainer() {
16+
console.log(`Self Driven Content Rendering`)
17+
return (
18+
<div className="container">
19+
<h3>'Self Driven' Content Container (NO RE-RENDERS)</h3>
20+
<SelfDrivenFormContainer />
21+
<SelfDrivenDisplayContainer />
1122
</div>
1223
);
1324
};
Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,27 @@
1+
import { useAppFastContextField } from "../App";
2+
13
type Props = {
4+
fieldName?: string;
25
label?: string;
3-
value: string;
6+
value?: string;
47
};
5-
export default function Display({label, value}: Readonly<Props>) {
6-
console.log(`${label} display change`)
8+
export function PropDrivenDisplay({label, value}: Readonly<Props>) {
9+
console.log(`Prop Driven ${label} display rendering`)
710
return (
811
<div className="value">
912
{label ? <label>{label} : </label> : null}
1013
<input value={value} readOnly style={{backgroundColor: "#eee", cursor: 'auto', border: 0}}/>
1114
</div>
1215
);
1316
};
17+
18+
export function SelfDrivenDisplay({ fieldName, label }: Readonly<Props>) {
19+
console.log(`Self Driven ${label} display rendering`)
20+
const [value] = useAppFastContextField(fieldName as string);
21+
return (
22+
<div className="value">
23+
{label ? <label>{label} : </label> : null}
24+
<input value={value as string} readOnly style={{backgroundColor: "#eee", cursor: 'auto', border: 0}}/>
25+
</div>
26+
);
27+
};
Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,26 @@
11
import { useAppFastContextField } from "../App";
2-
import Display from "./Display";
2+
import { PropDrivenDisplay, SelfDrivenDisplay } from "./Display";
33

4-
export default function DisplayContainer() {
5-
console.log(`Display Rendering`)
4+
export function PropDrivenDisplayContainer() {
5+
console.log(`Prop Driven Display Rendering`)
66
const [first] = useAppFastContextField('first');
77
const [last] = useAppFastContextField('last');
88
return (
99
<div className="container">
10-
<h4>DisplayContainer</h4>
11-
<Display label="First Name" value={first as string} />
12-
<Display label="Last Name" value={last as string} />
10+
<h4>'Prop Driven' Display (Container AND children re-render on field changes)</h4>
11+
<PropDrivenDisplay label="First Name" value={first as string} />
12+
<PropDrivenDisplay label="Last Name" value={last as string} />
13+
</div>
14+
);
15+
};
16+
17+
export function SelfDrivenDisplayContainer() {
18+
console.log(`Self Driven Display Rendering`)
19+
return (
20+
<div className="container">
21+
<h4>'Self Driven' Display (NO RE-RENDERS - only children re-render on field changes)</h4>
22+
<SelfDrivenDisplay fieldName="first" label="First Name" />
23+
<SelfDrivenDisplay fieldName="last" label="Last Name" />
1324
</div>
1425
);
1526
};
Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,26 @@
11
import { useAppFastContextField } from "../App";
2-
import TextInput from "./TextInput";
2+
import { FormDrivenTextInput, SelfDrivenTextInput } from "./TextInput";
33

4-
export default function FormContainer() {
5-
console.log(`Form Rendering`)
4+
export function PropDrivenFormContainer() {
5+
console.log(`Prop Driven Form Rendering`)
66
const [firstName, setFirstName] = useAppFastContextField("first");
77
const [lastName, setLastName] = useAppFastContextField("last");
88
return (
99
<div className="container">
10-
<h4>FormContainer</h4>
11-
<TextInput value={firstName as string} label='First Name' onChange={setFirstName}/>
12-
<TextInput value={lastName as string} label='Last Name' onChange={setLastName} />
10+
<h4>'Prop Driven' Input Form (Form AND children re-render on field changes)</h4>
11+
<FormDrivenTextInput value={firstName as string} label='First Name' onChange={setFirstName}/>
12+
<FormDrivenTextInput value={lastName as string} label='Last Name' onChange={setLastName} />
13+
</div>
14+
);
15+
};
16+
17+
export function SelfDrivenFormContainer() {
18+
console.log(`Self Driven Form Rendering`)
19+
return (
20+
<div className="container">
21+
<h4>'Self Driven' Input Form (NO RE-RENDERS - only children re-render on field changes)</h4>
22+
<SelfDrivenTextInput fieldName="first" label='First Name'/>
23+
<SelfDrivenTextInput fieldName="last" label='Last Name'/>
1324
</div>
1425
);
1526
};
Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
1+
import { useAppFastContextField } from "../App";
2+
13
type Props = {
4+
fieldName?: string;
25
label?: string;
3-
value: string;
4-
onChange: (value: string) => void;
6+
value?: string;
7+
onChange?: (value: string) => void;
58
};
6-
export default function TextInput( { label = '', value, onChange}: Readonly<Props> ) {
9+
export function FormDrivenTextInput( { label = '', value, onChange = (v) => {}}: Readonly<Props> ) {
10+
console.log(`Prop Driven ${label} input rendering`)
711
return (
812
<div className="field">
913
{label ? <label>{label} : </label> : null}
@@ -14,3 +18,18 @@ export default function TextInput( { label = '', value, onChange}: Readonly<Prop
1418
</div>
1519
);
1620
};
21+
22+
23+
export function SelfDrivenTextInput( { fieldName, label }: Readonly<Props> ) {
24+
console.log(`Self Driven ${label} input rendering`)
25+
const [value, setValue] = useAppFastContextField(fieldName as string);
26+
return (
27+
<div className="field">
28+
{label ? <label>{label} : </label> : null}
29+
<input
30+
value={value as string}
31+
onChange={(e) => setValue(e.target.value)}
32+
/>
33+
</div>
34+
);
35+
};
Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
1-
import ContentContainer from "../components/ContentContainer";
2-
1+
import { PropDrivenContentContainer, SelfDrivenContentContainer } from "../components/ContentContainer";
32

43
export default function MainPage() {
54
console.log(`Page Rendering`)
65
return (
76
<div className="container">
8-
<h2>Page</h2>
9-
<ContentContainer />
7+
<h2>Page (NO RE-RENDERS)</h2>
8+
<div style={{display: 'flex', justifyContent: 'space-around', margin: '1em'}}>
9+
<PropDrivenContentContainer />
10+
<SelfDrivenContentContainer />
11+
</div>
1012
</div>
1113
);
1214
}

0 commit comments

Comments
 (0)