Created
April 23, 2020 23:42
-
-
Save cassidoo/45c79852ba44cd6a61a8276f682b7ef8 to your computer and use it in GitHub Desktop.
An example of a local storage hook
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 { 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