node.js - 如何实现 localStorage 同步器
问题描述
我想用我的 App.js 实现我的 useLocalStorage 文件,但我无法理解如何做到这一点,因为我还在学习。
使用LocalStorage.js
import { useState, useEffect } from "react";
function useLocalStorage(key, firstValue = null) {
const initialValue = localStorage.getItem(key) || firstValue;
const [item, setItem] = useState(initialValue);
useEffect(function setKeyInLocalStorage() {
console.debug("hooks useLocalStorage useEffect", "item=", item);
if (item === null) {
localStorage.removeItem(key);
} else {
localStorage.setItem(key, item);
}
}, [key, item]);
return [item, setItem];
}
export default useLocalStorage;
应用程序.js
import useLocalStorage from "./hooks/useLocalStorage";
const App = () => {
const dispatch = useDispatch();
useEffect(() => {
dispatch(axios());
}, []);
return (
<BrowserRouter>
<LastLocationProvider>
<Navbar />
<Routes />
<Footer />
</LastLocationProvider>
</BrowserRouter>
);
};
export default App;
您可以通过的任何提示或知识将不胜感激!
解决方案
推荐阅读
- python - 具有多个估计器的 Sklearn 管道
- php - Laravel 使用条件表名对两个表进行连接查询
- python - 有没有办法从 yfinance 重命名数据框中的这些列?
- jquery - 如何在 mvc 视图中获取输入值?
- html - 我无法将位置粘性应用到子元素
- amazon-web-services - 使用 Make 文件为 Flutter 和 Golang 生成 gRPC 文件
- r - 从 R 中的 oracle 数据库读取数据的更快方法
- javascript - S3 使用 DeleteMarker 删除版本化对象
- asp.net-mvc - 我需要从 Asp.netCore MVC 中的 url 获取锚点和 ambressent 值
- blockchain - 如何在 Binance 中生成新的存款 Tether 地址