Skip to content

Commit 02aa7a8

Browse files
author
Antoine RICHARD
committed
fix: convert carousel to Class component (lab 3)
1 parent b5d5025 commit 02aa7a8

File tree

1 file changed

+29
-13
lines changed

1 file changed

+29
-13
lines changed
Lines changed: 29 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import React from 'react';
12
import { ArrowBack, ArrowForward } from '@mui/icons-material';
23
import MuiFab from '@mui/material/Fab';
34
import { PersonCard } from './PersonCard';
@@ -6,17 +7,32 @@ interface CarouselProps {
67
people: People;
78
}
89

9-
export function Carousel({ people }: CarouselProps) {
10-
const randomPerson = people[Math.floor(Math.random() * people.length)];
11-
return (
12-
<section className="carousel">
13-
<MuiFab size="medium" color="default" aria-label="previous">
14-
<ArrowBack />
15-
</MuiFab>
16-
<PersonCard person={randomPerson} />
17-
<MuiFab size="medium" color="default" aria-label="previous">
18-
<ArrowForward />
19-
</MuiFab>
20-
</section>
21-
);
10+
interface CarouselState {
11+
randomPersonIndex: number;
12+
}
13+
14+
export class Carousel extends React.Component<CarouselProps, CarouselState> {
15+
constructor(props: CarouselProps) {
16+
super(props);
17+
this.state = {
18+
randomPersonIndex: Math.floor(Math.random() * props.people.length),
19+
};
20+
}
21+
22+
render() {
23+
const { people } = this.props;
24+
const randomPerson = people[this.state.randomPersonIndex];
25+
26+
return (
27+
<section className="carousel">
28+
<MuiFab size="medium" color="default" aria-label="previous">
29+
<ArrowBack />
30+
</MuiFab>
31+
<PersonCard person={randomPerson} />
32+
<MuiFab size="medium" color="default" aria-label="next">
33+
<ArrowForward />
34+
</MuiFab>
35+
</section>
36+
);
37+
}
2238
}

0 commit comments

Comments
 (0)