Skip to content

Criar React Hooks para obter dados de uma determinada Key #5

@ipetinate

Description

@ipetinate

Criar um hook customizado do react para obter a Key do localstorage, semelhante a esse:

import { useEffect } from 'react'

import { read } from 'localstorage-helpr'

/**
 * Hook for get LS data and set to a component state, this hook call the `read` method from `localstorage-helpr` lib on component mount and execution.
 * @param {string} key LS Key for get Data
 * @param {Function} setState Function to set LS data to componente state (ex: reference for `setState` or `setMyData` on component)
 */
export const useGetLocalStorageData = (key, setState) => {
  useEffect(() => {
    setState(read(key))
    // eslint-disable-next-line react-hooks/exhaustive-deps
  }, [])
}

export default useGetLocalStorageData

Metadata

Metadata

Assignees

No one assigned

    Labels

    documentationImprovements or additions to documentationenhancementNew feature or requestgood first issueGood for newcomershelp wantedExtra attention is needed

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions