Proyectos de Subversion LeadersLinked - Antes de SPA

Rev

Rev 6748 | Mostrar el archivo completo | | | Autoría | Ultima modificación | Ver Log |

Rev 6748 Rev 6753
Línea 1... Línea 1...
1
import { useState, useEffect } from 'react'
1
import { useState, useCallback } from 'react'
Línea 2... Línea 2...
2
 
2
 
3
const useLocalStorage = (key, defaultValue) => {
3
const useLocalStorage = ({ stateName, initialValue }) => {
4
  const [value, setValue] = useState(() => {
-
 
Línea -... Línea 4...
-
 
4
  const name = `persist/${stateName}`
5
    let currentValue
5
 
-
 
6
  const getFromStorage = (name, defaultValue) => {
6
 
7
    try {
-
 
8
      const val = JSON.parse(localStorage.getItem(name) + '')
-
 
9
      if (val !== null) {
7
    try {
10
        return val
8
      currentValue = JSON.parse(
11
      } else {
9
        localStorage.getItem(key) || String(defaultValue)
12
        localStorage.setItem(name, JSON.stringify(defaultValue))
10
      )
13
      }
11
    } catch (error) {
14
    } catch {
-
 
15
      return defaultValue
Línea 12... Línea 16...
12
      currentValue = defaultValue
16
    }
13
    }
-
 
Línea -... Línea 17...
-
 
17
  }
14
 
18
 
15
    return currentValue
19
  const [state, setState] = useState(getFromStorage(name, initialValue))
-
 
20
 
-
 
21
  const setValue = useCallback(
-
 
22
    (value) => {
16
  })
23
      localStorage.setItem(name, JSON.stringify(value))
-
 
24
      setState(value)
Línea 17... Línea 25...
17
 
25
      console.log(name, value)
18
  useEffect(() => {
26
    },
Línea 19... Línea 27...
19
    localStorage.setItem(key, JSON.stringify(value))
27
    [name]