Skip to content

Commit

Permalink
fix: ssr issues in usePersistState
Browse files Browse the repository at this point in the history
  • Loading branch information
totraev committed Nov 23, 2024
1 parent 2c193fa commit 60bda63
Showing 1 changed file with 6 additions and 1 deletion.
7 changes: 6 additions & 1 deletion src/hooks/usePersistState.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,14 @@

import { type SetStateAction, type Dispatch, useState, useEffect } from "react";

export function usePersistState<S>(key: string, storage: Storage, initialState?: S): [S, Dispatch<SetStateAction<S>>] {
export function usePersistState<S>(key: string, storage: Storage, initialState: S): [S, Dispatch<SetStateAction<S>>] {
function getDefaultState() {
const defaultValue = typeof initialState === "function" ? (initialState as () => S)() : initialState;

if (typeof storage === "undefined") {
return defaultValue;
}

const persistValue = storage.getItem(key);
const defaultState = persistValue ? (JSON.parse(persistValue) as S) : null;

Expand Down

0 comments on commit 60bda63

Please sign in to comment.