Skip to content

Commit 6ff0fae

Browse files
committed
refactor: 使用hooks重构TypingCard组件
1 parent 0d3878b commit 6ff0fae

File tree

1 file changed

+33
-30
lines changed

1 file changed

+33
-30
lines changed
Lines changed: 33 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,42 @@
1-
import React from "react";
1+
import React, { useRef, useEffect } from "react";
22
import { Card } from "antd";
33
import Typing from "@/utils/typing";
44

5-
class TypingCard extends React.Component {
6-
static defaultProps = {
7-
title: "何时使用",
8-
source: "",
9-
height: 136,
10-
};
11-
componentDidMount() {
5+
const TypingCard = (props) => {
6+
const { title, height, source } = props;
7+
8+
const sourceEl = useRef();
9+
const outputEl = useRef();
10+
11+
useEffect(() => {
1212
const typing = new Typing({
13-
source: this.source,
14-
output: this.output,
13+
source: sourceEl.current,
14+
output: outputEl.current,
1515
delay: 30,
1616
});
1717
typing.start();
18-
}
19-
render() {
20-
return (
21-
<Card
22-
bordered={false}
23-
className="card-item"
24-
title={this.props.title}
25-
style={{ minHeight: this.props.height }}
26-
id={this.props.id}
27-
>
28-
<div
29-
style={{ display: "none" }}
30-
ref={(el) => (this.source = el)}
31-
dangerouslySetInnerHTML={{ __html: this.props.source }}
32-
/>
33-
<div ref={(el) => (this.output = el)} />
34-
</Card>
35-
);
36-
}
37-
}
18+
}, []);
19+
return (
20+
<Card
21+
bordered={false}
22+
className="card-item"
23+
title={title}
24+
style={{ minHeight: height }}
25+
>
26+
<div
27+
style={{ display: "none" }}
28+
ref={sourceEl}
29+
dangerouslySetInnerHTML={{ __html: source }}
30+
/>
31+
<div ref={outputEl} />
32+
</Card>
33+
);
34+
};
35+
36+
TypingCard.prototype = {
37+
title: "",
38+
source: "",
39+
height: 136,
40+
};
3841

3942
export default TypingCard;

0 commit comments

Comments
 (0)