Skip to content

Commit 6899018

Browse files
sai6855Michael-Hutchinson
authored andcommitted
[docs][material-ui][Autocomplete] Refactor asynchronous loading demo (mui#43630)
1 parent 0b4dd2d commit 6899018

File tree

2 files changed

+26
-54
lines changed

2 files changed

+26
-54
lines changed

docs/data/material/components/autocomplete/Asynchronous.js

Lines changed: 13 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -14,44 +14,30 @@ function sleep(duration) {
1414
export default function Asynchronous() {
1515
const [open, setOpen] = React.useState(false);
1616
const [options, setOptions] = React.useState([]);
17-
const loading = open && options.length === 0;
18-
19-
React.useEffect(() => {
20-
let active = true;
21-
22-
if (!loading) {
23-
return undefined;
24-
}
17+
const [loading, setLoading] = React.useState(false);
2518

19+
const handleOpen = () => {
20+
setOpen(true);
2621
(async () => {
22+
setLoading(true);
2723
await sleep(1e3); // For demo purposes.
24+
setLoading(false);
2825

29-
if (active) {
30-
setOptions([...topFilms]);
31-
}
26+
setOptions([...topFilms]);
3227
})();
28+
};
3329

34-
return () => {
35-
active = false;
36-
};
37-
}, [loading]);
38-
39-
React.useEffect(() => {
40-
if (!open) {
41-
setOptions([]);
42-
}
43-
}, [open]);
30+
const handleClose = () => {
31+
setOpen(false);
32+
setOptions([]);
33+
};
4434

4535
return (
4636
<Autocomplete
4737
sx={{ width: 300 }}
4838
open={open}
49-
onOpen={() => {
50-
setOpen(true);
51-
}}
52-
onClose={() => {
53-
setOpen(false);
54-
}}
39+
onOpen={handleOpen}
40+
onClose={handleClose}
5541
isOptionEqualToValue={(option, value) => option.title === value.title}
5642
getOptionLabel={(option) => option.title}
5743
options={options}

docs/data/material/components/autocomplete/Asynchronous.tsx

Lines changed: 13 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -19,44 +19,30 @@ function sleep(duration: number): Promise<void> {
1919
export default function Asynchronous() {
2020
const [open, setOpen] = React.useState(false);
2121
const [options, setOptions] = React.useState<readonly Film[]>([]);
22-
const loading = open && options.length === 0;
23-
24-
React.useEffect(() => {
25-
let active = true;
26-
27-
if (!loading) {
28-
return undefined;
29-
}
22+
const [loading, setLoading] = React.useState(false);
3023

24+
const handleOpen = () => {
25+
setOpen(true);
3126
(async () => {
27+
setLoading(true);
3228
await sleep(1e3); // For demo purposes.
29+
setLoading(false);
3330

34-
if (active) {
35-
setOptions([...topFilms]);
36-
}
31+
setOptions([...topFilms]);
3732
})();
33+
};
3834

39-
return () => {
40-
active = false;
41-
};
42-
}, [loading]);
43-
44-
React.useEffect(() => {
45-
if (!open) {
46-
setOptions([]);
47-
}
48-
}, [open]);
35+
const handleClose = () => {
36+
setOpen(false);
37+
setOptions([]);
38+
};
4939

5040
return (
5141
<Autocomplete
5242
sx={{ width: 300 }}
5343
open={open}
54-
onOpen={() => {
55-
setOpen(true);
56-
}}
57-
onClose={() => {
58-
setOpen(false);
59-
}}
44+
onOpen={handleOpen}
45+
onClose={handleClose}
6046
isOptionEqualToValue={(option, value) => option.title === value.title}
6147
getOptionLabel={(option) => option.title}
6248
options={options}

0 commit comments

Comments
 (0)