Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

using useLocalStorage to setValue during unMount does not work #832

Open
iamrommel opened this issue Dec 12, 2019 · 1 comment
Open

using useLocalStorage to setValue during unMount does not work #832

iamrommel opened this issue Dec 12, 2019 · 1 comment

Comments

@iamrommel
Copy link

What is the current behavior?
I was calling the setValue for localStorage inside useUnmount, but it does not store the value. It gets value, or set the value on mount or when button was clicked, but does not work during useUnmount. What I want is store store only the value when the component is removed.

Steps to reproduce it and if possible a minimal demo of the problem. Your bug will get fixed much faster if we can run your code and it doesn't have extra dependencies other than react-use. Paste the link to your JSFiddle or CodeSandbox example below:

import React, { useContext as useContextCore } from 'react'
import { useList, useLocalStorage, useUnmount, useMount } from 'react-use'
import { StorageKeys } from '../../../config/StorageKeys'

const context = React.createContext()
const CoreProvider = context.Provider

export const Provider = ({ children }) => {

  const [logs, storeLogs] = useLocalStorage(StorageKeys.processLogs, [])
  const [list, { insertAt, clear, set }] = useList([])

  useUnmount(() => {
    storeLogs(list)
  })

  useMount(() => {
    set(logs)
    console.log(logs, 'logs mounted here')
  })


  return <CoreProvider value={{ list, insertAt, clear }}>{children}</CoreProvider>
}




What is the expected behavior?
It should update the localStorage on unMount

@JoeDuncko
Copy link

Hi all! @react-hookz/web, the new library by one of react-use's former maintainers (background here and here) has a new implementation of useLocalStorage called use useLocalStorageValue that should solve this issue

For those interested, there's an official migration guide for migrating from react-use to @react-hookz/web.

Hope this helps!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants