-
-
Notifications
You must be signed in to change notification settings - Fork 40
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
107 additions
and
6 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,12 @@ | ||
import useLocalStorageState from './src/useLocalStorageState' | ||
import useSsrLocalStorageState from './src/useSsrLocalStorageState' | ||
import createLocalStorageStateHook from './src/createLocalStorageStateHook' | ||
import createSsrLocalStorageStateHook from './src/createSsrLocalStorageStateHook' | ||
|
||
export default useLocalStorageState | ||
export { useLocalStorageState, createLocalStorageStateHook } | ||
export { | ||
useLocalStorageState, | ||
useSsrLocalStorageState, | ||
createLocalStorageStateHook, | ||
createSsrLocalStorageStateHook, | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import withSsr from './withSsr' | ||
import createLocalStorageStateHook from './createLocalStorageStateHook' | ||
|
||
export default function createSsrLocalStorageStateHook( | ||
...args: Parameters<typeof createLocalStorageStateHook> | ||
): ReturnType<typeof createLocalStorageStateHook> { | ||
return withSsr(createLocalStorageStateHook(...args)) | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
export default function unwrapValue<T>(value: T | (() => T)): T { | ||
const isCallable = (value: unknown): value is () => T => typeof value === 'function' | ||
return isCallable(value) ? value() : value | ||
} |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
import withSsr from './withSsr' | ||
import useLocalStorageState from './useLocalStorageState' | ||
|
||
export default withSsr(useLocalStorageState) |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import unwrapValue from './unwrapValue' | ||
import useLocalStorageState from './useLocalStorageState' | ||
import { useEffect, useLayoutEffect, useRef, useState } from 'react' | ||
|
||
export default function withSsr<T extends typeof useLocalStorageState>(useLocalStorageHook: T): T { | ||
const useLocalStorageStateWithSsr = (...args: Parameters<T>): ReturnType<T> => { | ||
const defaultValue = args[1] | ||
const isFirstRender = useRef(true) | ||
const [_, forceUpdate] = useState(false) | ||
const isServer = typeof window === 'undefined' | ||
const useIsomorphicEffect = isServer ? useEffect : useLayoutEffect | ||
|
||
// asdf | ||
// eslint-disable-next-line prefer-spread | ||
const localStorageState = useLocalStorageHook.apply(undefined, args) | ||
|
||
useIsomorphicEffect(() => { | ||
if (unwrapValue(defaultValue) !== localStorageState[0]) { | ||
forceUpdate(true) | ||
} | ||
|
||
isFirstRender.current = false | ||
}, []) | ||
|
||
if (isFirstRender.current) { | ||
return [ | ||
unwrapValue(defaultValue), | ||
localStorageState[1], | ||
localStorageState[2], | ||
] as ReturnType<T> | ||
} | ||
|
||
return localStorageState as ReturnType<T> | ||
} | ||
|
||
return useLocalStorageStateWithSsr as unknown as T | ||
} |
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