window.onload = function() {
const initialItems = [...items]
for (let i = 0; i < localStorage.length; i++) {
initialItems.push(JSON.parse(localStorage.getItem(items.itemId)));
}
setItems(items);
};
import CommentForm from './CommentForm'
import CommentList from './CommentList'
import CommentatorsTop from "./CommentatorsTop";
import {useState} from 'react';
function CommentWidget() {
const [items, setItems] = useState([]);
window.onload = function() {
const initialItems = [...items]
for (let i = 0; i < localStorage.length; i++) {
initialItems.push(JSON.parse(localStorage.getItem(items.itemId)));
}
setItems(items);
};
function addItem(item) {
const newItems = [...items]
newItems.push(item)
setItems(newItems);
const storageNewItems = JSON.stringify(newItems);
localStorage.setItem(item.itemId, storageNewItems);
}
function removeCommentItem(position, itemId) {
if (window.confirm('Удалить?')) {
const newListItem = [...items]
newListItem.splice(position, 1);
localStorage.removeItem(itemId);
setItems(newListItem);
}
}
return (
<div className="CommentWidget">
<div className="CommentForm-wrap">
<CommentForm addNewItem={addItem}/>
<CommentList comments={items} removeCommentItem={removeCommentItem} />
</div>
<CommentatorsTop names={items}/>
</div>
)
}
export default CommentWidget;
const storageItems = []
for (let i = 0; i < localStorage.length; i++) {
storageItems.push(JSON.parse(localStorage.getItem(localStorage.key(i))));
}
и затем передать массив в state: setItems(items);