Skip to content

How to automate testing? #60

Answered by soofstad
PauloStanize-NHT asked this question in Q&A
Discussion options

You must be logged in to vote

Hello @PauloStanize-NHT

Think i found the issue. Atleast got your snippet to work as intended.

The problem is that your are not formating the "token" as a JSON-string when writing to localStorage.
You wrote it as thisismytoken, then when the library try to load this, as seen in this code:

function useLocalStorage<T>(key: string, initialValue: T): [T, (v: T) => void] {
  const [storedValue, setStoredValue] = useState<T>(() => {
    const item = localStorage.getItem(key)
    try {
      return item ? JSON.parse(item) : initialValue
    } catch (error) {
      return initialValue
    }
  })

It fails, causing it to return the "initialValue". JSON-strings must be quoted to be parsed as a valid…

Replies: 2 comments 3 replies

Comment options

You must be logged in to vote
2 replies
@PauloStanize-NHT
Comment options

@sebastianvitterso
Comment options

Comment options

You must be logged in to vote
1 reply
@PauloStanize-NHT
Comment options

Answer selected by PauloStanize-NHT
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
3 participants