Skip to content

Instantly share code, notes, and snippets.

View jogilvyt's full-sized avatar

Jack Taylor jogilvyt

View GitHub Profile
@jogilvyt
jogilvyt / App.js
Created January 19, 2021 15:20
Simple React counter application
import { useState } from "react";
function App() {
const [count, setCount] = useState(0);
return (
<div>
<h1>Counter</h1>
<p>Number: {count}</p>
<button onClick={() => setCount(count - 1)}>-1</button>
@jogilvyt
jogilvyt / App.js
Created January 19, 2021 15:38
React counter app with localStorage
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>
@jogilvyt
jogilvyt / App.js
Created January 19, 2021 15:53
A React counter app with localStorage
import { useState, useEffect } from "react";
function App() {
const key = "count";
const [count, setCount] = useState(() => {
const persistedValue = window.localStorage.getItem(key);
return persistedValue !== null ? JSON.parse(persistedValue) : 0;
});
@jogilvyt
jogilvyt / useStateWithLocalStorage.js
Created January 19, 2021 16:35
React hook to store state in localStorage
import { useState, useEffect } from "react";
const useStateWithLocalStorage = (defaultValue, key) => {
const [value, setValue] = useState(() => {
const persistedValue = window.localStorage.getItem(key);
return persistedValue !== null ? JSON.parse(persistedValue) : defaultValue;
});
useEffect(() => {
@jogilvyt
jogilvyt / App.js
Created January 19, 2021 16:39
React counter with localStorage hook
import useStateWithLocalStorage from "../hooks/useStateWithLocalStorage";
function App() {
const [count, setCount] = useStateWithLocalStorage(0, "count");
return (
<div>
<h1>Counter</h1>
<p>Number: {count}</p>
<button onClick={() => setCount(count - 1)}>-1</button>
@jogilvyt
jogilvyt / useStateWithLocalStorage.test.js
Last active January 19, 2021 17:04
Tests for useStateWithLocalStorage
import { renderHook, act } from "@testing-library/react-hooks";
import useStateWithLocalStorage from "./useStateWithLocalStorage";
const TEST_KEY = "key";
const TEST_VALUE = { test: "test" };
describe("useStateWithLocalStorage", () => {
it("should set localStorage with default value", () => {
renderHook(() => useStateWithLocalStorage(TEST_VALUE, TEST_KEY));
@jogilvyt
jogilvyt / App.test.js
Created January 19, 2021 17:09
End to end test for counter component
it("should initialise with the value stored in localStorage", () => {
// set the localStorage to a value other than 0
localStorage.setItem("count", 15);
// render the App and check it initialises to the value in localStorage
render(<App />);
screen.getByText(/Number: 15/);
// click the increment button and check the localStorage is updated
const incrementButton = screen.getByText(/\+1/);
@jogilvyt
jogilvyt / setupTests.js
Created January 19, 2021 17:14
LocalStorageMock to enable testing of localStorage on older versions of jest
class LocalStorageMock {
constructor() {
this.store = {};
}
clear() {
this.store = {};
}
getItem(key) {
import { useState } from "react";
import { v4 as uuid } from "uuid";
import "./ToDo.css";
import ToDoList from "./ToDoList";
import Form from "./Form";
const ToDo = () => {
const [todos, setTodos] = useState([{ id: uuid(), value: "A to do item" }]);
import ToDoItem from "../ToDoItem";
const ToDoList = ({ todos, handleDelete }) => {
return (
<ul className="list">
{todos.length ? (
todos.map(item => (
<ToDoItem item={item} handleDelete={handleDelete} key={item.id} />
))
) : (