Skip to content

Commit 2302e8c

Browse files
committed
updated formation
1 parent 78fee92 commit 2302e8c

3 files changed

Lines changed: 28 additions & 113 deletions

File tree

src/examples/use-effect-1/ExampleUseEffect103.tsx

Lines changed: 12 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -4,82 +4,41 @@ import cloneDeep from "lodash.clonedeep";
44
import { useLog } from "../../commons/ExampleBloc";
55

66
const SubComponent = ({
7-
value,
8-
setValue,
9-
otherValue,
10-
setOtherValue
7+
parentValue,
8+
setParentValue,
119
}: {
12-
value: number;
13-
setValue: (newValue: number) => void;
14-
otherValue: { str: string };
15-
setOtherValue: (newValue: { str: string }) => void;
10+
parentValue: { str: string };
11+
setParentValue: (newValue: { str: string }) => void;
1612
}) => {
1713
const log = useLog();
18-
const [state, setState] = React.useState<number>(value);
19-
const [otherState, setOtherState] = React.useState<any>(otherValue);
14+
const [state, setState] = React.useState<any>(parentValue);
2015
const [shouldSetSuperState, setShouldSetSuperState] = React.useState<boolean>(false);
2116

2217
log("render");
2318

24-
React.useEffect(() => {
25-
setState(value);
26-
}, [value, setState]);
2719
React.useEffect(() => {
2820
log("use effect");
29-
if (shouldSetSuperState) setOtherValue(otherValue);
30-
else setOtherState(otherValue);
31-
}, [otherValue, setOtherState, setOtherValue, shouldSetSuperState, log]);
21+
if (shouldSetSuperState) setParentValue(state);
22+
setState(parentValue);
23+
}, [parentValue, setParentValue, state, setState, shouldSetSuperState, log]);
3224

3325
return (
3426
<>
35-
<pre>{JSON.stringify({ value, state, otherState, shouldSetSuperState }, null, 2)}</pre>
27+
<pre>{JSON.stringify({ state, shouldSetSuperState }, null, 2)}</pre>
3628
<ul>
37-
<li>
38-
<ActionButton
39-
label="Increment state"
40-
onClick={() => {
41-
setState(state => state + 1);
42-
}}
43-
/>
44-
</li>
45-
<li>
46-
<ActionButton
47-
label="Increment super state"
48-
onClick={() => {
49-
setValue(value + 1);
50-
}}
51-
/>
52-
</li>
5329
<li>
5430
<ActionButton
5531
label="Change other state"
5632
onClick={() => {
57-
setOtherState({ someProps: "zefzefz" });
58-
}}
59-
/>
60-
</li>
61-
<li>
62-
<ActionButton
63-
label="Change super other state"
64-
onClick={() => {
65-
setOtherValue({ str: "zezfez" });
66-
}}
67-
/>
68-
</li>
69-
<li>
70-
<ActionButton
71-
label="Reset super state"
72-
onClick={() => {
73-
setValue(0);
74-
setOtherValue({ str: "" });
33+
setState({ someProps: "zefzefz" });
7534
}}
7635
/>
7736
</li>
7837
<li>
7938
<ActionButton
8039
label="Change state option"
8140
onClick={() => {
82-
setShouldSetSuperState(state => !state);
41+
setShouldSetSuperState((state) => !state);
8342
}}
8443
/>
8544
</li>
@@ -89,17 +48,9 @@ const SubComponent = ({
8948
};
9049

9150
const ExampleUseState106 = () => {
92-
const [counter, setCounter] = React.useState<number>(0);
9351
const [str, setStr] = React.useState<{ str: string }>({ str: "" });
9452

95-
return (
96-
<SubComponent
97-
value={counter}
98-
setValue={setCounter}
99-
otherValue={cloneDeep(str)}
100-
setOtherValue={setStr}
101-
/>
102-
);
53+
return <SubComponent parentValue={cloneDeep(str)} setParentValue={setStr} />;
10354
};
10455

10556
export default ExampleUseState106;

src/examples/use-ref-1/ExampleUseRef101.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,10 @@ const ExampleUseRef101 = () => {
1414
<ul>
1515
<li>
1616
<ActionButton
17-
label="Do nothing ref-wise"
17+
label="Log ref value"
1818
onClick={() => {
1919
/* NOOP */
20-
log("Ref: ", { ref });
20+
console.log("Ref: ", ref.current);
2121
}}
2222
/>
2323
</li>

src/examples/use-ref-1/ExampleUseRef103.tsx

Lines changed: 14 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -1,63 +1,27 @@
1-
import React from "react";
1+
import React, { useRef } from "react";
22
import ActionButton from "../../commons/ActionButton";
3-
import { useLog } from "../../commons/ExampleBloc";
4-
5-
const MyComponent = ({ value }: { value: number }) => {
6-
return <pre>Number inside MyComponent: {value}</pre>;
7-
};
8-
9-
function renderOnce<P extends object>(
10-
Component: React.ComponentType<P>
11-
): React.FC<P> {
12-
return function HocComponent(props: P) {
13-
const ref = React.useRef<React.ReactElement | null>(null);
14-
15-
if (ref.current === null) {
16-
ref.current = <Component {...props} />;
17-
}
18-
19-
return ref.current ? ref.current : null;
20-
};
21-
}
22-
23-
const RenderOnceMyComponent = renderOnce(MyComponent);
243

254
const ExampleUseRef103 = () => {
26-
const log = useLog();
27-
28-
const [state, setState] = React.useState<number>(0);
29-
log("virtual-render", { state });
5+
const ref = useRef<HTMLInputElement | null>(null);
306

317
return (
328
<>
33-
<pre>Number outside MyComponent: {state}</pre>
34-
35-
<div>
36-
<RenderOnceMyComponent value={state} />
37-
</div>
38-
9+
<input ref={ref}></input>
3910
<ul>
4011
<li>
4112
<ActionButton
42-
label="Do nothing state-wise"
13+
label="Set input value"
4314
onClick={() => {
44-
/* NOOP */
45-
}}
46-
/>
47-
</li>
48-
<li>
49-
<ActionButton
50-
label="Increment"
51-
onClick={() => {
52-
setState(state + 1);
53-
}}
54-
/>
55-
</li>
56-
<li>
57-
<ActionButton
58-
label="Reset to 0"
59-
onClick={() => {
60-
setState(0);
15+
if (ref.current === null) {
16+
return;
17+
}
18+
19+
const str = prompt("Value?");
20+
if (str === null) {
21+
return;
22+
}
23+
24+
ref.current.value = str;
6125
}}
6226
/>
6327
</li>

0 commit comments

Comments
 (0)