New frontend and tools
This commit is contained in:
23
dewey/hooks/useLocalStorage.ts
Normal file
23
dewey/hooks/useLocalStorage.ts
Normal file
@ -0,0 +1,23 @@
|
||||
import { useState, useEffect } from 'react'
|
||||
|
||||
export default function useLocalStorage(key, initialValue) {
|
||||
const [storedValue, setStoredValue] = useState(() => {
|
||||
try {
|
||||
const item = window.localStorage.getItem(key)
|
||||
return item ? JSON.parse(item) : initialValue
|
||||
} catch (error) {
|
||||
console.log(error)
|
||||
return initialValue
|
||||
}
|
||||
})
|
||||
|
||||
useEffect(() => {
|
||||
try {
|
||||
window.localStorage.setItem(key, JSON.stringify(storedValue))
|
||||
} catch (error) {
|
||||
console.log(error)
|
||||
}
|
||||
}, [key, storedValue])
|
||||
|
||||
return [storedValue, setStoredValue]
|
||||
}
|
14
dewey/hooks/useSocket.ts
Normal file
14
dewey/hooks/useSocket.ts
Normal file
@ -0,0 +1,14 @@
|
||||
import { useEffect, useState } from 'react'
|
||||
import io from 'socket.io-client'
|
||||
|
||||
export default function useSocket() {
|
||||
const [socket, setSocket] = useState<any>(null)
|
||||
|
||||
useEffect(() => {
|
||||
const newSocket = io('http://localhost:5001')
|
||||
setSocket(newSocket)
|
||||
return () => newSocket.close()
|
||||
}, [])
|
||||
|
||||
return socket
|
||||
}
|
Reference in New Issue
Block a user