Skip to content

Instantly share code, notes, and snippets.

@OFranke

OFranke/02.js Secret

Last active January 26, 2021 15:52
Show Gist options
  • Save OFranke/dbebd209d509bfde021f006b64f7a203 to your computer and use it in GitHub Desktop.
Save OFranke/dbebd209d509bfde021f006b64f7a203 to your computer and use it in GitHub Desktop.
// 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