Skip to content

Instantly share code, notes, and snippets.

@cassidoo
Created April 23, 2020 23:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cassidoo/45c79852ba44cd6a61a8276f682b7ef8 to your computer and use it in GitHub Desktop.
Save cassidoo/45c79852ba44cd6a61a8276f682b7ef8 to your computer and use it in GitHub Desktop.
An example of a local storage hook
import { useEffect } from "react";
import { useAppReducer } from "../AppContext";
export default function useLocal() {
const dispatch = useAppReducer();
useEffect(() => {
dispatch({
type: "GET_FROM_LOCAL_STATE",
somePieceOfState: localStorage.getItem("some_state") || "defaultvalue",
});
}, [dispatch]);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment