forked from kentcdodds/beginners-guide-to-react
-
Notifications
You must be signed in to change notification settings - Fork 0
/
11-event-handlers.html
45 lines (38 loc) · 1.16 KB
/
11-event-handlers.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
<body>
<div id="root"></div>
<script src="https://unpkg.com/react@16.12.0/umd/react.development.js"></script>
<script src="https://unpkg.com/react-dom@16.12.0/umd/react-dom.development.js"></script>
<script src="https://unpkg.com/@babel/standalone@7.8.3/babel.js"></script>
<script type="text/babel">
const rootElement = document.getElementById('root')
const state = {eventCount: 0, username: ''}
function App() {
function handleClick() {
setState({eventCount: state.eventCount + 1})
}
function handleChange(event) {
setState({username: event.target.value})
}
return (
<div>
<p>There have been {state.eventCount} events.</p>
<p>
<button onClick={handleClick}>Click Me</button>
</p>
<p>You typed: {state.username}</p>
<p>
<input onChange={handleChange} />
</p>
</div>
)
}
function setState(newState) {
Object.assign(state, newState)
renderApp()
}
function renderApp() {
ReactDOM.render(<App />, document.getElementById('root'))
}
renderApp()
</script>
</body>