Skip to content

Commit 3cd19b7

Browse files
committed
CH1 class component example
1 parent de535d2 commit 3cd19b7

File tree

2 files changed

+65
-0
lines changed

2 files changed

+65
-0
lines changed

Chapter1/Class-component/Alert.js

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
import { Component } from "react";
2+
3+
export class Alert extends Component {
4+
constructor(props) {
5+
super(props);
6+
this.state = { visible: true };
7+
}
8+
render() {
9+
const {
10+
type = "information",
11+
heading,
12+
children,
13+
closable,
14+
onClose
15+
} = this.props;
16+
17+
if (!this.state.visible) {
18+
return null;
19+
}
20+
const handleCloseClick = () => {
21+
this.setState({ visible: false });
22+
if (onClose) {
23+
onClose();
24+
}
25+
};
26+
return (
27+
<div>
28+
<div>
29+
<span
30+
role="img"
31+
aria-label={type === "warning" ? "Warning" : "Information"}
32+
>
33+
{type === "warning" ? "⚠" : "ℹ️"}
34+
</span>
35+
<span>{heading}</span>
36+
</div>
37+
{closable && (
38+
<button aria-label="Close" onClick={handleCloseClick}>
39+
<span role="img" aria-label="Close">
40+
41+
</span>
42+
</button>
43+
)}
44+
<div>{children}</div>
45+
</div>
46+
);
47+
}
48+
}

Chapter1/Class-component/App.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import { Alert } from "./Alert";
2+
import "./styles.css";
3+
4+
export default function App() {
5+
return (
6+
<div className="App">
7+
<Alert
8+
type="information"
9+
heading="Success"
10+
closable
11+
onClose={() => console.log("closed")}
12+
>
13+
Everything is really good!
14+
</Alert>
15+
</div>
16+
);
17+
}

0 commit comments

Comments
 (0)