forked from kentcdodds/beginners-guide-to-react
-
Notifications
You must be signed in to change notification settings - Fork 0
/
15-effect-deps.html
41 lines (36 loc) · 1.17 KB
/
15-effect-deps.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
<body>
<div id="root"></div>
<script src="https://unpkg.com/[email protected]/umd/react.development.js"></script>
<script src="https://unpkg.com/[email protected]/umd/react-dom.development.js"></script>
<script src="https://unpkg.com/@babel/[email protected]/babel.js"></script>
<script type="text/babel">
function Greeting() {
const [name, setName] = React.useState(
() => window.localStorage.getItem('name') || '',
)
React.useEffect(() => {
window.localStorage.setItem('name', name)
}, [name])
const handleChange = event => setName(event.target.value)
return (
<div>
<form>
<label htmlFor="name">Name: </label>
<input value={name} onChange={handleChange} id="name" />
</form>
{name ? <strong>Hello {name}</strong> : 'Please type your name'}
</div>
)
}
function App() {
const [count, setCount] = React.useState(0)
return (
<>
<button onClick={() => setCount(c => c + 1)}>{count}</button>
<Greeting />
</>
)
}
ReactDOM.render(<App />, document.getElementById('root'))
</script>
</body>