Im doing next js application in app router 14.1.4 , typescript and fetching news by params slug
if news not found, i want to redirect to 404 page.
how i do that ?
by the way this is the code and fetching news using axios. if news not found backend return 404.
"use client";
import { getNewsById } from "@/utils/data";
import NewsPage from "./NewsPage";
import { useEffect, useState } from "react";
import { News } from "@/utils/definitions";
export default function Page({ params }: { params: { slug: string } }) {
const [news, setNews] = useState<News | undefined>();
useEffect(() => {
const loadNews = async () => {
const fetchedNews = await getNewsById(parseInt(params.slug));
if (!fetchedNews) {
// TODO: if news not found, redirect to 404
}
setNews(fetchedNews);
};
loadNews();
}, [params.slug]);
return news && <NewsPage news={news} />;
}
if news not found, i want to redirect to 404 page.
how i do that ?
by the way this is the code and fetching news using axios. if news not found backend return 404.
"use client";
import { getNewsById } from "@/utils/data";
import NewsPage from "./NewsPage";
import { useEffect, useState } from "react";
import { News } from "@/utils/definitions";
export default function Page({ params }: { params: { slug: string } }) {
const [news, setNews] = useState<News | undefined>();
useEffect(() => {
const loadNews = async () => {
const fetchedNews = await getNewsById(parseInt(params.slug));
if (!fetchedNews) {
// TODO: if news not found, redirect to 404
}
setNews(fetchedNews);
};
loadNews();
}, [params.slug]);
return news && <NewsPage news={news} />;
}