-
-
Save OFranke/dbebd209d509bfde021f006b64f7a203 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// useEffect: persistent state | |
// http://localhost:3000/isolated/exercise/02.js | |
import * as React from 'react' | |
function Greeting({initialName = ''}) { | |
const [name, setName] = React.useState( | |
window.localStorage.getItem('name') || initialName, | |
) | |
React.useEffect(() => { | |
window.localStorage.setItem('name', name) | |
console.log('\x1b[33m%s\x1b[0m', '%c >> rendered') | |
}) | |
function handleChange(event) { | |
setName(event.target.value) | |
} | |
return ( | |
<div> | |
<form> | |
<label htmlFor="name">Name: </label> | |
<input onChange={handleChange} id="name" /> | |
</form> | |
{name ? <strong>Hello {name}</strong> : 'Please type your name'} | |
</div> | |
) | |
} | |
function App() { | |
return <Greeting /> | |
} | |
export default App |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment