forked from RubyLouvre/anu
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex3.html
122 lines (108 loc) · 2.71 KB
/
index3.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
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<!---
<script src="https://cdn.bootcss.com/react/16.6.3/umd/react.development.js"></script>
<script src="https://cdn.bootcss.com/react-dom/16.6.3/umd/react-dom.development.js"></script>
-->
<script src="./dist/React.js"></script>
<script type='text/javascript' src="./lib/babel.js"></script>
</head>
<body>
<div id='root' class="root">
</div>
<script type='text/babel'>
var container = document.getElementById('root');
var div = container;
if (!window.ReactDOM) {
window.ReactDOM = React;
}
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(){
try{
a()
}catch(e){
console.log(e,"catch")
}
}
};
};
const themes = {
green: {
foreground: '#000000',
background: 'green',
},
red: {
foreground: '#ffffff',
background: 'red',
},
};
const ThemeContext = React.createContext(
themes.red // default value
);
class ThemedButton extends React.Component {
render() {
let props = this.props;
let theme = this.context;
console.log("ThemedButton", props, theme)
return (
<button
{...props}
style={{backgroundColor: theme.background}}
/>
);
}
}
ThemedButton.contextType = ThemeContext;
function Toolbar(props) {
return (
<ThemedButton onClick={props.changeTheme}>
Change Theme
</ThemedButton>
);
}
class App extends React.Component {
constructor(props) {
super(props);
this.state = {
theme: themes.green,
};
console.log("变色")
this.toggleTheme = () => {
console.log(this.state.theme, '1111')
this.setState(state => ({
theme:
state.theme === themes.red
? themes.green
: themes.red,
}));
};
}
render() {
// The ThemedButton button inside the ThemeProvider
// uses the theme from state while the one outside uses
// the default red theme
return (
<div>
<ThemeContext.Provider value={this.state.theme}>
<Toolbar changeTheme={this.toggleTheme} />
</ThemeContext.Provider>
<section>
<ThemedButton>下面的</ThemedButton>
</section>
</div>
);
}
}
ReactDOM.render(<App />, container)
</script>
</html>