File tree Expand file tree Collapse file tree 1 file changed +20
-6
lines changed Expand file tree Collapse file tree 1 file changed +20
-6
lines changed Original file line number Diff line number Diff line change @@ -9,31 +9,45 @@ const RESOURCES_URL =
9
9
"https://michaelcurrin.github.io/dev-resources/resources/javascript/packages/react/" ;
10
10
11
11
function TextSample ( ) {
12
- const [ value , setValue ] = React . useState ( "" ) ;
12
+ const persistedValue = localStorage . getItem ( "name" ) ?? "" ;
13
+ const [ value , setValue ] = React . useState ( persistedValue ) ;
13
14
14
- const onChange = ( event ) => {
15
+ const onInput = ( event ) => {
15
16
setValue ( event . target . value ) ;
16
17
} ;
17
18
18
- const msg = value ? `Welcome, ${ value } ` : "Tell me your name" ;
19
+ React . useEffect ( ( ) => {
20
+ localStorage . setItem ( "name" , value ) ;
21
+ } ) ;
22
+
23
+ const msg = value
24
+ ? html `< p > Welcome, < b > ${ value } </ b > </ p > `
25
+ : "Tell me your name" ;
19
26
20
27
return html `
21
28
< div >
22
- < label hmltfor ="name-input "> Name: </ label >
29
+ < label hmltfor ="name-input "> Name: </ label >
23
30
24
31
< span > </ span >
25
32
26
33
< input
27
34
id ="name-input "
28
- placeholder ="World "
29
35
value =${ value }
30
- onChange =${ onChange }
36
+ onInput =${ onInput }
37
+ placeholder="World"
31
38
/>
32
39
33
40
< br />
34
41
< br />
35
42
36
43
< div > ${ msg } </ div >
44
+
45
+ < p >
46
+ < i
47
+ > This component persists data in < code > localStorage</ code > , so it will
48
+ remember values across a page refresh or restarting the browser.</ i
49
+ >
50
+ </ p >
37
51
</ div >
38
52
` ;
39
53
}
You can’t perform that action at this time.
0 commit comments