Dzień dobry,
Mam problem podczas nauki reacta.
{ id: 1, title: "Wiadomosc numer 1", body: "Zawartosc wiadomosci numer 1" },
{ id: 2, title: "Wiadomosc numer 2", body: "Zawartosc wiadomosci numer 2" },
{ id: 3, title: "Wiadomosc numer 3", body: "Zawartosc wiadomosci numer 3" },
];
setInterval(() => {
const index = data.length + 1;
console.log("aktualizacja");
data.push({
id: index,
title: `Wiadomosc numer ${index}`,
body: `Zawartosc wiadomosci numer ${index}`,
});
}, 2000);
const App = () => {
const [comments, setComments] = useState(data);
console.log(comments);
const getData = () => {
setComments(data);
};
useEffect(() => {
setInterval(() => {
console.log("update z setinterval");
getData();
}, 5000);
}, []);
let divs = comments.map((comment) => (
<div key={comment.id}>
<h4>{comment.title}</h4>
<div>{comment.body}</div>
</div>
));
return (
<div className="App">
<>{divs}</>
</div>
);
};
React nie rerenderuje divów po użyciu map()
.
Robi to tylko na początku, a potem nie aktualizuje wiadomości z tablicy.
Może mi ktoś wytłumaczyć jak to działa?