Skip to content

Commit dd69e4e

Browse files
committed
scenario 'order'
1 parent 473d665 commit dd69e4e

4 files changed

Lines changed: 153 additions & 4 deletions

File tree

src/App.tsx

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import examplesUseEffect1 from "./examples/use-effect-1";
55
import examplesUseRef1 from "./examples/use-ref-1";
66
import examplesHooksOrder from "./examples/hooks-order";
77
import examplesMemoization from "./examples/memoization";
8+
import examplesScenarioOrder from "./examples/scenario-order";
89
import logo from "./logo.svg";
910
import ExampleBloc from "./commons/ExampleBloc";
1011
import { IExample } from "./commons/types";
@@ -19,7 +20,8 @@ const App: React.FC = () => {
1920
.concat(examplesUseEffect1)
2021
.concat(examplesUseRef1)
2122
.concat(examplesHooksOrder)
22-
.concat(examplesMemoization);
23+
.concat(examplesMemoization)
24+
.concat(examplesScenarioOrder);
2325

2426
return (
2527
<div className="App">
@@ -90,8 +92,7 @@ const App: React.FC = () => {
9092
export default App;
9193

9294
/*
93-
94-
Idées:
95+
Autres idées:
9596
- pq utiliser un hook custom pour les contextes (permet d'éviter le typecheck sur null & co)
9697
- react context instantiation or mounting
9798
*/

src/examples/memoization/index.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ const examples: IExample[] = [
8080
<>
8181
<strong>React.memo</strong> is not a hook, but a HOC that will perform a
8282
shallow comparison on the input props before triggering a render of the
83-
wrapped component. SOmetime, it can be cleaner to use it instead of the
83+
wrapped component. Sometimes, it can be cleaner to use it instead of the
8484
more general-pruprpose <strong>useMemo()</strong>
8585
</>
8686
),
Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
import React from "react";
2+
3+
interface IOrderItem {
4+
id: string;
5+
quantity: number;
6+
}
7+
8+
interface IOrderContent {
9+
items: IOrderItem[];
10+
}
11+
12+
const MyItemAudi = ({
13+
orderContent,
14+
setOrderContent
15+
}: {
16+
orderContent: IOrderContent;
17+
setOrderContent: (orderContent: IOrderContent) => void;
18+
}) => {
19+
const quantity = orderContent.items.reduce(
20+
(acc, item) => acc + (item.id === "audi" ? item.quantity : 0),
21+
0
22+
);
23+
24+
const more = () => {
25+
setOrderContent({
26+
items: orderContent.items
27+
.filter(item => item.id !== "audi")
28+
.concat({ id: "audi", quantity: quantity + 1 })
29+
});
30+
};
31+
32+
return (
33+
<>
34+
Audi cars are very nice [...more text...]. Quantity: {quantity}.{" "}
35+
<button onClick={more}>Add more</button>
36+
</>
37+
);
38+
};
39+
40+
const MyItemBmw = ({
41+
orderContent,
42+
setOrderContent
43+
}: {
44+
orderContent: IOrderContent;
45+
setOrderContent: (orderContent: IOrderContent) => void;
46+
}) => {
47+
const quantity = orderContent.items.reduce(
48+
(acc, item) => acc + (item.id === "bmw" ? item.quantity : 0),
49+
0
50+
);
51+
52+
const more = () => {
53+
setOrderContent({
54+
items: orderContent.items
55+
.filter(item => item.id !== "bmw")
56+
.concat({ id: "bmw", quantity: quantity + 1 })
57+
});
58+
};
59+
60+
return (
61+
<>
62+
BMW are nice too, although a little bit tacky [...more text...]. Quantity:{" "}
63+
{quantity}. <button onClick={more}>Add more</button>
64+
</>
65+
);
66+
};
67+
68+
const AllItems = ({
69+
orderContent,
70+
setOrderContent
71+
}: {
72+
orderContent: IOrderContent;
73+
setOrderContent: (orderContent: IOrderContent) => void;
74+
}) => {
75+
const [selected, setSelected] = React.useState<string>("audi");
76+
77+
return (
78+
<div>
79+
<div>
80+
Select car:{" "}
81+
<select
82+
onChange={e => {
83+
setSelected(e.currentTarget.value);
84+
}}
85+
>
86+
<option value="audi">Audi</option>
87+
<option value="bmw">BMW</option>
88+
</select>
89+
</div>
90+
91+
<div>
92+
Car description:
93+
<div>
94+
{selected === "audi" && (
95+
<MyItemAudi
96+
orderContent={orderContent}
97+
setOrderContent={setOrderContent}
98+
/>
99+
)}
100+
{selected === "bmw" && (
101+
<MyItemBmw
102+
orderContent={orderContent}
103+
setOrderContent={setOrderContent}
104+
/>
105+
)}
106+
</div>
107+
</div>
108+
109+
<button onClick={() => setOrderContent({ items: [] })}>reset</button>
110+
</div>
111+
);
112+
};
113+
114+
const OrderManager = () => {
115+
const [orderContent, setOrderContent] = React.useState<IOrderContent>({
116+
items: []
117+
});
118+
119+
return (
120+
<>
121+
<pre>{JSON.stringify(orderContent, null, 2)}</pre>
122+
<AllItems orderContent={orderContent} setOrderContent={setOrderContent} />
123+
</>
124+
);
125+
};
126+
127+
export default OrderManager;
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/* eslint import/no-webpack-loader-syntax: off */
2+
import BeforeCode from "!raw-loader!./Before";
3+
import React from "react";
4+
import { IExample } from "../../commons/types";
5+
import Before from "./Before";
6+
7+
const examples: IExample[] = [
8+
{
9+
title: "Scenario 'order': before",
10+
Component: Before,
11+
code: BeforeCode,
12+
preface: (
13+
<>
14+
Can you find a way to technically improve this very simple app by using
15+
what you learned aboot React and hooks ?
16+
</>
17+
)
18+
}
19+
];
20+
21+
export default examples;

0 commit comments

Comments
 (0)