I am fetching data using RTK Query but when errors occur I create a toast using react-toastify library that shows a toast message multiple times how to prevent this? when I consol log I see that part of 'if' condition render multiple times.
import FoodBox from "../components/FoodBox";
import styled from "styled-components";
import { useFetchFoodsQuery } from "../store";
import { useSelector } from "react-redux";
import Skeleton from "./Skeleton";
import { toast } from "react-toastify";
const Foods = styled.div`
overflow-y: auto;
overflow-x: hidden;
height: 370px;
padding: 0 30px;
`;
function FoodList({ shopId }) {
let { data: foods, error, isFetching } = useFetchFoodsQuery(shopId);
const user = useSelector((state) => state.user.currentUser);
let content;
if (isFetching) {
content = (
<Foods>
<Skeleton w="22.5rem" h="3.5rem" times={5} />
</Foods>
);
} else if (error) {
toast('Error');
return <div>Error in loading food</div>;
} else {
if (!user) {
foods = foods?.filter((food) => food.isVisible);
}
content = (
<Foods>
{foods?.map((food) => (
<FoodBox key={food?._id} food={food} />
))}
</Foods>
);
}
return content;
}
export default FoodList;