【发布时间】:2020-07-20 17:55:01
【问题描述】:
我目前正在创建一个天气应用程序,您可以在其中存储您喜欢的位置,这些位置被推送到一个数组中,而数组又存储在本地存储中。
现在,我想在不同的组件中显示数组的值,但是在我的代码中,我不断发现对象可能为 null 的错误。
export const FavoritesList: React.FC<any> = () => {
// const storageFavorites = localStorage.getItem('favorites');
const lsOutput = document.getElementById('lsOutput');
for (let i = 0; i < localStorage.length; i++) {
const key: any = localStorage.key(i);
const value: any = localStorage.getItem(key);
// console.log(value);
value.forEach(
(item: any) => (lsOutput.innerHTML += `<li>${item}</li><br/>`)
);
}
return (
<div>
<ul id="lsOutput"></ul>
</div>
);
};
它专门指向 lsOutput.innerHTML
我做错了什么? 我目前已将所有内容分配为 any 只是为了让它首先工作。 非常感谢您!
【问题讨论】:
标签: javascript reactjs typescript eslint-config-airbnb react-tsx