mirror of
https://github.com/jakejarvis/jarv.is.git
synced 2026-09-08 20:25:34 -04:00
clean up useLocalStorage hook
This commit is contained in:
+11
-13
@@ -2,20 +2,18 @@
|
||||
|
||||
import { useEffect, useState } from "react";
|
||||
|
||||
const getInitialState = (query: string, defaultState?: boolean): boolean => {
|
||||
if (defaultState !== undefined) {
|
||||
return defaultState;
|
||||
}
|
||||
|
||||
if (typeof window !== "undefined") {
|
||||
return window.matchMedia(query).matches;
|
||||
}
|
||||
|
||||
return false;
|
||||
};
|
||||
|
||||
const useMedia = (query: string, defaultState?: boolean): boolean => {
|
||||
const [state, setState] = useState(getInitialState(query, defaultState));
|
||||
const [state, setState] = useState(() => {
|
||||
if (defaultState !== undefined) {
|
||||
return defaultState;
|
||||
}
|
||||
|
||||
if (typeof window !== "undefined") {
|
||||
return window.matchMedia(query).matches;
|
||||
}
|
||||
|
||||
return false;
|
||||
});
|
||||
|
||||
useEffect(() => {
|
||||
let mounted = true;
|
||||
|
||||
Reference in New Issue
Block a user