Created
January 19, 2021 15:38
-
-
Save jogilvyt/6f2069a01ec9b87498b877ccd113b565 to your computer and use it in GitHub Desktop.
React counter app with localStorage
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
import { useState } from "react"; | |
function App() { | |
const [count, setCount] = useState(() => { | |
const persistedValue = window.localStorage.getItem("count"); | |
return persistedValue !== null ? JSON.parse(persistedValue) : 0; | |
}); | |
return ( | |
<div> | |
<h1>Counter</h1> | |
<p>Number: {count}</p> | |
<button onClick={() => setCount(count - 1)}>-1</button> | |
<button onClick={() => setCount(count + 1)}>+1</button> | |
</div> | |
); | |
} | |
export default App; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment