【发布时间】:2022-01-10 16:57:28
【问题描述】:
我正在尝试为计数器存储一个数字,该计数器初始化为 0,每次用户上传照片时计数为 1。我在配置文件 console.log orderHook.orderCount 中未定义。在 localStorage 我得到 {count:0} 的 console.log
非常感谢任何帮助!
LocalStorage.js:
import { useState, useEffect } from 'react';
function getStorageValue(key, defaultValue) {
// getting stored value
const saved = localStorage.getItem(key);
const initial = JSON.parse(saved); // unexpected character @ line 1 column 1
// const initial = JSON.stringify(saved);
return initial || defaultValue;
}
export const useLocalStorage = (key, defaultValue) => {
const [value, setValue] = useState(() => {
console.log(key)
return getStorageValue(key, defaultValue);
});
useEffect(() => {
localStorage.setItem(key, JSON.stringify(value));
}, [key, value]);
return [value, setValue];
};
Count.js:自定义钩子
import { useLocalStorage } from "../../Utilities/localStorage/localStorage"; // Local storage hook
function useOrderCountHook() {
const [orderCount, setOrderCount] = useLocalStorage({count: 0}); // The profile image
const changeOrderCount = () => {
setOrderCount({ count: orderCount.count + 1 })
}
return { orderCount, changeOrderCount };
// return [orderCount, changeOrderCount];
}
export default useOrderCountHook;
Profile.js:使用自定义钩子
const orderHook = useOrderCountHook(); // How the photo count hook is called
console.log(typeof (orderHook.orderCount)) // undefined
console.log(orderHook.orderCount) // undefined
const handleUploadChange = e => { // Input onChange
if (e.target.files[0]) {
setImage(e.target.files[0]);
} else { }
return handleCountChange()
};
const handleCountChange = () => { // Function for custom count hook
if (orderHook.orderCount === undefined) {
return
} else {
return orderHook.orderCount
}
}
return (
{
currentUser ?
<input
type="file"
for="Upload Image"
accept="image/*"
name="image"
id="file"
onChange={handleUploadChange}
onClick={handleUploadChange}
style={{ display: "none" }}
/>
: ''
}
<i className="bi bi-camera">
{orderHook.orderCount === undefined ?
<span className="banner-list-font mx-1">0 photos</span>
:
<span className="banner-list-font mx-1">{orderHook.orderCount.count} photos</span>
}
</i>
**更新**
所以我设法让它工作,但是当我退出用户并重新登录时,如果我尝试上传照片,我会收到错误无法读取未定义的属性(读取“计数”)。这是 defaultValue 道具,并且该控制台记录为未定义。我认为问题在于计数被存储为对象?
localStorage.js:
import { useState, useEffect } from 'react';
// function getStorageValue(key, defaultValue) {
// const saved = localStorage.getItem(key);
// console.log(saved, defaultValue, key)// => null undefined {count:0}
// const initial = JSON.parse(saved);
// console.log(initial)
// // const initial = JSON.stringify(saved);
// // return key || defaultValue;
// return initial || defaultValue;
// }
function getStorageValue(key, defaultValue) {
const saved = localStorage.getItem(key);
console.log(saved, defaultValue, key)// => null undefined {count:0}
if (saved === null) {
return defaultValue;
}
return JSON.parse(saved);
}
export const useLocalStorage = (key, defaultValue) => {
console.log(key, defaultValue)// => {count:0} undefined
const [value, setValue] = useState(() => {
return getStorageValue(key, defaultValue);
});
useEffect(() => {
localStorage.setItem(key, JSON.stringify(value));
}, [key, value]);
return [value, setValue];
};
Profile.js:
return (
<input
type="file"
for="Upload Image"
accept="image/*"
name="image"
id="file"
onChange={e => { orderHook.changeOrderCount(e); handleUploadChange(e) }}
onClick={handleUploadChange}
style={{ display: "none" }}
/>
<i className="bi bi-camera"></i>
{orderHook.orderCount === undefined || orderHook.orderCount === null ?
<span className="banner-list-font mx-1">0 photos</span>
:
<span className="banner-list-font mx-1">{orderHook.orderCount.count} photos</span>
}
【问题讨论】:
标签: javascript reactjs react-hooks local-storage