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(0); | |
return ( | |
<div> | |
<h1>Counter</h1> | |
<p>Number: {count}</p> | |
<button onClick={() => setCount(count - 1)}>-1</button> |
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> |
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, 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; | |
}); |
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, 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(() => { |
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 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> |
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 { 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)); |
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
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/); |
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
class LocalStorageMock { | |
constructor() { | |
this.store = {}; | |
} | |
clear() { | |
this.store = {}; | |
} | |
getItem(key) { |
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"; | |
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" }]); |
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 ToDoItem from "../ToDoItem"; | |
const ToDoList = ({ todos, handleDelete }) => { | |
return ( | |
<ul className="list"> | |
{todos.length ? ( | |
todos.map(item => ( | |
<ToDoItem item={item} handleDelete={handleDelete} key={item.id} /> | |
)) | |
) : ( |
OlderNewer