forked from RubyLouvre/anu
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index7.html
128 lines (115 loc) · 3.81 KB
/
index7.html
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<script type='text/javascript' src="./dist/React.js"></script>
<!-- <script type='text/javascript' src="./test/react.js"></script>
<script type='text/javascript' src="./test/react-dom.js"></script>-->
<script src="test/babel.js"></script>
</head>
<body>
<div id="test"></div>
<div id="content"></div>
</body>
<script type="text/babel">
var container = document.getElementById("test");
var expect = function(a) {
return {
toBe: function(b) {
console.log(a,"vs", b, a === b)
},
toEqual(b){
console.log(a,"vs", b, a +""=== b+"")
},
toThrow(b){
try{
a()
}catch(e){
console.log(e,"vs", b, e.message +""=== b+"")
}
}
}
}
class Child extends React.Component {
constructor(props) {
super(props);
}
render() {
console.log(this.props.value);
return null;
}
}
function Wrapper(props) {
return <Child {...props} ref={props.forwardedRef} />;
}
const RefForwardingComponent = React.forwardRef((props, ref) => (
<Wrapper {...props} forwardedRef={ref} />
));
let setRefCount = 0;
let ref;
const setRef = r => {
setRefCount++;
ref = r;
};
ReactDOM.render(<RefForwardingComponent ref={setRef} value={123} />,container);
console.log(ref)
expect(ref instanceof Child).toBe(true);
expect(setRefCount).toBe(1);
ReactDOM.render(<RefForwardingComponent ref={setRef} value={456} />,container);
expect(ref instanceof Child).toBe(true);
expect(setRefCount).toBe(1);
/*
var list = [];
class Static extends React.Component {
componentDidMount() {
list.push("static did mount");
}
componentWillUpdate() {
list.push("static will update");
}
componentDidUpdate() {
list.push("static did update");
}
render() {
list.push("static render");
return <div>{this.props.children}</div>;
}
}
class Component extends React.Component {
render() {
if (this.props.flipped) {
return (
<div>
<Static
ref={function (b) {
list.push(b === null ? "null 222" : "instance 222");
}}
>
B
</Static>
</div>
);
} else {
return (
<div>
<Static
ref={function (a) {
list.push(a === null ? "null 111" : "instance 111");
}}
>
A
</Static>
</div>
);
}
}
}
var container = document.createElement("div");
ReactDOM.render(<Component flipped={false} />, container);
ReactDOM.render(<Component flipped={true} />, container);
expect(list).toEqual(["static render", "static did mount", "instance 111", "null 111",
"static will update", "static render", "static did update", "instance 222"]);
*/
</script>
</html>