forked from aladin002dz/Workshop-React-WordpressRestApi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEg2-State.html
56 lines (50 loc) · 2.04 KB
/
Eg2-State.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
<html>
<head>
<!-- Modules to run React -->
<!-- module to get React elements, mostly components -->
<script src="https://unpkg.com/react@16/umd/react.development.js"></script>
<!-- module to render react elements into the web page -->
<script src="https://unpkg.com/react-dom@16/umd/react-dom.development.js"></script>
<!-- module to use RJX and ES6 -->
<!--just to add some nice looking :) -->
<link rel="stylesheet" href="style.css">
</head>
<body>
<!-- where react elements will be rendered -->
<div id="root"></div>
<script type="text/babel">
/* React component with a "State" **********************************************/
/* State change will trigger the refresh of the component */
class MyComponent extends React.Component {
constructor(){
super();
this.state = {
message: "Hello (from state)"
};
this.updateMessage = this.updateMessage.bind(this);
}
/* function to change the state */
updateMessage() {
this.setState({
message: "Hello (from changed state)"
});
}
/* every react component must have a render method to define the UI */
render() {
return (
<div>
<h1>{this.state.message}!</h1>
<button onClick={this.updateMessage}>Click me!</button>
</div>
)
}
}
/* Rendering the React component into the DOM ****************************/
ReactDOM.render(
<MyComponent />,
document.getElementById("root")
);
</script>
</body>
</html>