I have a page coded in React with NextJS hooks, when I try to render a page the error says what's in the title, I presume because the object which I'm mapping is empty/undefined on first load. I added "?" to every map I have on the page and it's still giving me this error... I noticed that if I stay on that page after it gives me error and press "Ctrl + shift + r" the page loads normally. What could be causing this?
import {Fragment, useEffect} from "react";
import Head from "next/head";
import DashboardPage from "../../../../components/components/dashboard/DashboardPage";
import LayoutDashboard from "../../../../components/layout/LayoutDashboard";
import React from "react";
import Pusher from "pusher-js";
import useSWR, {mutate} from "swr";
const fetcher = async () => {
const response1 = await fetch("API");
const data1 = await response1.json();
const props = {
data: data1,
};
return props;
};
export default function Dashboard(props) {
const {data, error} = useSWR("data", fetcher);
useEffect(() => {
//Pusher.logToConsole = true;
var pusher = new Pusher("pshr", {
cluster: "eu",
});
const channel = pusher.subscribe("chnl");
channel.bind("chnl", function (data) {
console.log(data);
mutate("data");
});
}, []);
if (error) return "Error";
if (!data) return "Loading";
console.log(data);
return (
<Fragment>
<Head>
<title>Dashboard</title>
<link rel="icon" href="/favicon.ico" />
</Head>
<LayoutDashboard restaurantData={props?.restaurantData[0]}>
<DashboardPage
orders={data?.showItemsOnOrder}
dashboardCards={data?.dashboardCardInfo}
ordersGraph={data?.dashboardGraph}
/>
</LayoutDashboard>
</Fragment>
);
}
export async function getStaticPaths() {
const response = await fetch(`API`);
const data = await response.json();
const tables = [];
for (var i = 1; i <= data[0].restaurantTables; i++) {
tables.push({
restaurant: data[0].queryName,
tableNr: i.toString(),
});
}
return {
paths: tables.map((table) => {
return {
params: {
restaurantName: table.restaurant,
tableNr: table.tableNr,
},
};
}),
fallback: false,
};
}
export async function getStaticProps() {
const response = await fetch(`API`);
const data = await response.json();
return {
props: {
restaurantData: data,
},
revalidate: 1,
};
}
EDIT I recognized that the site works normally if I go straight to the link that I want... It stops working when I'm calling components with Link tags in nextJS then it throws an error that it's in title... So if I go straight to the link everything works as expected maybe that is also the reason that the page works if I click on my link and then refresh it... So what could be the problem with Link tag? This is my code for it:
<Link
href={{
pathname: "/restaurant/restaurantName/dashboard/",
query: {restaurantName: restaurantName},
}}
>
<div
className={
router.pathname == "/restaurant/[restaurantName]/dashboard"
? "text-blue-600 bg-gray-50"
: "text-gray-700 "
}
>
<div className="flex p-3 space-x-4 0 hover:bg-gray-50 hover:text-blue-600 cursor-pointer">
<DonutLargeIcon className=" text-gray-300" />
<p className="">Dashbord</p>
</div>
</div>
</Link>