Problem: react view is not updated
I have a state that has the following composition:
[
{
date: "12/10/2022",
data: [
{ id: "1", title_tax: "tax number one" },
{ id: "2", title_tax: "tax number two" },
],
},
{
date: "13/10/2022",
data: [{ id: "3", title_tax: "tax number three" }],
},
];
the data object is an array that stores more objects, this is usually where we add more items. The process of adding items to the context is fine and they are added correctly, but the user view does not update these new items.
In my component I have an array (cardsHistory) that basically receives the state I showed and then creates the visualization. I don't have any exceptions or anything, I'm just looking for a way to fix this.
interface IProps {
cardsHistory: IPropsHistoryCard[];
loadMoreHistories: () => void;
isData: boolean;
}
function ScrollHistory({ cardsHistory, loadMoreHistories, isData }: IProps) {
const histories: IPropsHistoryCard[] = cardsHistory;
console.log(histories);
return (
<InfiniteScroll pageStart={1} loadMore={loadMoreHistories} hasMore={isData}>
{histories && histories?.length > 0 ? (
histories.map((x: IPropsHistoryCard) => (
<HistoryCard key={x.date} date={x.date} data={x.data} />
))
) : (
<Typography>No history to display</Typography>
)}
</InfiniteScroll>
);
}
export default ScrollHistory;
just in case when i recieve a new item I'm using immer produce from immer
case DashboardActionTypes.addHistory:
return produce(state, (draft) => {
const newElement: IProcess = payload;
const dayIncluded = new Date(
newElement.startDateTime,
).toLocaleDateString();
const processElement: IProcess[] = [];
processElement.push(newElement);
if (draft.history.length === 0) {
const historyElement: IPropsHistoryCard = {
date: dayIncluded,
data: processElement,
};
draft.history.push(historyElement);
} else {
const index = draft.history
.map((object) => object.date)
.indexOf(dayIncluded);
if (index < 0) {
const historyElement: IPropsHistoryCard = {
date: dayIncluded,
data: processElement,
};
draft.history.push(historyElement);
} else {
const isInArray = draft.history[index].data.find(
(element) => element.id === newElement.id,
);
if (isInArray === undefined)
draft.history[index].data.push(newElement);
}
}
});