Skip to content

Breaking the rules of hooks #3

@jonrose-dev

Description

@jonrose-dev

useState is conditionally called which is against the rules of hooks: https://reactjs.org/docs/hooks-rules.html#only-call-hooks-at-the-top-level

export const useLocalStorage = (key: string) => {
  if (typeof window === 'undefined' || typeof key !== 'string' || !key) {
    return [null, (_: string) => null];
  }

  const [value, setValue] = useState<string>(window.localStorage.getItem(key) || '');

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions