Skip to content

Instantly share code, notes, and snippets.

@tekknolagi
Created November 3, 2023 20:30
Show Gist options
  • Save tekknolagi/895ad0bd87db19753a472c2e5a87d569 to your computer and use it in GitHub Desktop.
Save tekknolagi/895ad0bd87db19753a472c2e5a87d569 to your computer and use it in GitHub Desktop.
simple useReducer react setup with localStorage
import React, { useCallback, useEffect, useReducer } from "https://esm.sh/[email protected]"
const STATE_KEY = "mystate";
const INITIAL_STATE = { age: 42, name: null };
function loadState() {
return JSON.parse(localStorage.getItem(STATE_KEY)) || INITIAL_STATE;
}
function reducer(state, action) {
if (action.type === 'inc_age') {
return {
...state,
age: state.age + 1
};
} else if (action.type === 'set_name') {
return {
...state,
name: action.name,
};
} else if (action.type === 'clear') {
return INITIAL_STATE;
}
throw Error('Unknown action.');
}
const App = () => {
const [state, dispatch] = useReducer(reducer, null, loadState);
useEffect(() => {
localStorage.setItem(STATE_KEY, JSON.stringify(state));
}, [state]);
const handleNameChange = e => dispatch({type: 'set_name', name: e.target.value });
const handleAgeIncrement = e => dispatch({type: 'inc_age' });
const handleClearStorage = e => dispatch({type: 'clear'});
const formName = state.name === null ? "" : state.name;
const renderedName = (state.name === null || state.name === "") ? "unknown user" : state.name;
return (
<>
<input onChange={handleNameChange} value={formName} />
<button onClick={handleAgeIncrement}>Increment age</button>
<p>Hello {renderedName}! You are {state.age}.</p>
<button onClick={handleClearStorage}>Clear localStorage</button>
</>
);
};
export default App
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>React App</title>
<link rel="stylesheet" href="./style.css">
</head>
<body>
<div id="root"></div>
<script type="module" src="./main.tsx"></script>
</body>
</html>
import React from "https://esm.sh/[email protected]"
import { createRoot } from "https://esm.sh/[email protected]/client"
import App from "./app.tsx"
const root = createRoot(document.getElementById("root"))
root.render(<App />)
* {
padding: 0;
margin: 0;
line-height: 1.5;
}
div {
width: 100vw;
height: 100vh;
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
text-align: center;
}
h1 {
font-size: 32px;
font-family: Inter, sans-serif;
}
p {
font-size: 16px;
font-style: italic;
}
button {
font-size: 30px;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment