You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
|
|
import { useEffect, useState} from 'react'
/* const {message, setMessage} = useState(defaultValue) 1.message 可以通过自定义传入默认初始值 2.每次修改 message 数据的时候,都会自动往同步一份 */
export function useLocalStorage (key, defaultValue) { const [message, setMessage] = useState(defaultValue) // 每次只要 message 变化,就会自动同步到本地 localStorage
useEffect (() => { // setItem(keyname, value) 是添加键和值,如果对应的值存在,则更新该键对应的值。
window.localStorage.setItem(key, message) }, [message, key]) return [message, setMessage] }
|