Skip to content

Instantly share code, notes, and snippets.

@ilxanlar
Last active June 16, 2021 06:45
Show Gist options
  • Save ilxanlar/9434bf8fd849acafee4d11badce6968e to your computer and use it in GitHub Desktop.
Save ilxanlar/9434bf8fd849acafee4d11badce6968e to your computer and use it in GitHub Desktop.
import React, { useState, useEffect } from 'react'
function Example() {
// Read initial count from localStorage
const [count, setCount] = useState(localStorage.getItem('count') || 0)
// Similar to componentDidMount and componentDidUpdate:
useEffect(() => {
// Update the document title using the browser API
document.title = `You clicked ${count} times`
}, [count])
// Similar to componentDidMount and componentDidUpdate:
useEffect(() => {
// Store count value after every render
localStorage.setItem('count', count)
}, [count])
return (
<div>
<p>You clicked {count} times</p>
<button onClick={() => setCount(count + 1)}>
Click me
</button>
</div>
)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment