React中Portal的使用
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
import React from 'react';
import ReactDOM, { createPortal } from 'react-dom';
import './index.css';
// import App from './App';
// import Content from '../components/Dialog/Content';
 
class HelloFromPortal extends React.Component {
  render() {
    return (
      <div
        onClick={() => {
          alert('我爸应该知道我被点击了');
        }}
      >
        我是传送门里出来的Portal
      </div>
    );
  }
}
 
class AmISameAsPortal extends React.Component {
  render() {
    return (
      <div
        onClick={() => {
          alert('是不是传送门里出来呢?我知道被点击了');
        }}
      >
        是不是传送门出来的? not Portal
      </div>
    );
  }
}
 
class HelloReact extends React.Component {
  render() {
    return (
      <div>
        <h1>父组件</h1>
        <div
          onClick={() => {
            alert('YES  Dispaly');
          }}
        >
          {createPortal(
            <HelloFromPortal />,
            document.getElementById('another-root')
          )}
        </div>
        XXXX XXXX
        <div
          onClick={() => {
            alert('No display');
          }}
        >
          {ReactDOM.render(
            <AmISameAsPortal />,
            document.getElementById('another-container')
          )}
        </div>
      </div>
    );
  }
}
 
ReactDOM.render(<HelloReact />, document.getElementById('root'));

  

posted on   人鱼之森  阅读(334)  评论(0编辑  收藏  举报
努力加载评论中...
点击右上角即可分享
微信分享提示