skip to Main Content

I want to import a custom hook I just created named useLocalStorage but i’ve been getting this error:

The requested module ‘/src/hooks/useLocalStorage.jsx?t=1688798057310’ does not provide an export named ‘useLocalStorage’ (at App.jsx:12:10)___

This is the export statement

export default function useLocalStorage(key, value) {
  const saveValue = () => {
    localStorage.setItem(key, value)
  }
  return saveValue
}

This is the import statement

import { useLocalStorage } from './hooks/useLocalStorage'

3

Answers


  1. export default function useLocalStorage(key, value) {
     const saveValue = () => {
        localStorage.setItem(key, value)
     }
     return saveValue
    }
    

    then :

    import useLocalStorage from './hooks/useLocalStorage'
    

    OR

    export function useLocalStorage(key, value) {
     const saveValue = () => {
        localStorage.setItem(key, value)
     }
     return saveValue
    }
    

    then:

    import {useLocalStorage} from './hooks/useLocalStorage'
    
    Login or Signup to reply.
  2. You are using export default ... which doesn’t create a named export. Thus you cannot use it like a named export. (See the docs for details)

    Either change the import to

    import useLocalStorage from "./hooks/useLocalStorage"
    

    to import your default export as useLocalStorage

    or change your export to

    export function useLocalStorage(...) {
      ...
    }
    

    to create a named export.

    Login or Signup to reply.
  3. the import statement is trying to import a named export called useLocalStorage from the module /src/hooks/useLocalStorage.jsx?t=1688798057310, but that module does not provide a named export with that name. Instead, it provides a default export which is the function useLocalStorage.

    To fix this error, you can change the import statement to:

    import useLocalStorage from './hooks/useLocalStorage'
    

    This will import the default export of the module which is the useLocalStorage function.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search