Yuan.tsx 619 Bytes
Newer Older
jim's avatar
jim committed
1
import React from 'react';
陈帅's avatar
陈帅 committed
2
import { yuan } from '../components/Charts';
jim's avatar
jim committed
3 4 5
/**
 * 减少使用 dangerouslySetInnerHTML
 */
陈帅's avatar
陈帅 committed
6 7 8
export default class Yuan extends React.Component<{
  children: React.ReactText;
}> {
陈帅's avatar
陈帅 committed
9
  main: HTMLSpanElement | undefined | null;
陈帅's avatar
陈帅 committed
10

jim's avatar
jim committed
11
  componentDidMount() {
陈帅's avatar
陈帅 committed
12
    this.renderToHtml();
jim's avatar
jim committed
13
  }
陈帅's avatar
陈帅 committed
14

jim's avatar
jim committed
15
  componentDidUpdate() {
陈帅's avatar
陈帅 committed
16
    this.renderToHtml();
jim's avatar
jim committed
17
  }
陈帅's avatar
陈帅 committed
18

陈帅's avatar
陈帅 committed
19
  renderToHtml = () => {
陈帅's avatar
陈帅 committed
20
    const { children } = this.props;
jim's avatar
jim committed
21
    if (this.main) {
陈帅's avatar
陈帅 committed
22
      this.main.innerHTML = yuan(children);
jim's avatar
jim committed
23 24
    }
  };
陈帅's avatar
陈帅 committed
25

jim's avatar
jim committed
26 27 28 29 30 31 32 33 34 35
  render() {
    return (
      <span
        ref={ref => {
          this.main = ref;
        }}
      />
    );
  }
}