I am trying to create a website where the main page is showing list of card containing information of a restaurant
i tried to create folder named "detail" containing [id].jsx. this is the code for [id].jsx
export const getStaticPaths = async () => {
const res = await fetch(`https://restaurant-api.dicoding.dev/list`);
const data = await res.json();
const paths = data.map((restaurant) => {
return {
params: { id: restaurant.id },
};
});
return {
paths,
fallback: true,
};
};
export const getStaticProps = async ({ params }) => {
const id = params.id;
const res = await fetch(`https://restaurant-api.dicoding.dev/detail/${id}`);
const data = await res.json();
console.log(data);
return {
props: { restaurant: data },
};
};
const Details = ({ restaurant }) => {
return (
<div>
<h1>{restaurant.name}</h1>
<p>{restaurant.email}</p>
<p>{restaurant.website}</p>
<p>{restaurant.address.city}</p>
</div>
);
};
export default Details;
this is where i tried to access [id].jsx
"use client";
import { RestaurantCard } from "../components/card";
import { getRestaurants } from "../hooks/UseGetRestaurants";
import Link from "next/link";
export default function ListRestaurant() {
const data = getRestaurants();
if (!data || !data.restaurants) {
return null;
}
return (
<>
<div className="grid grid-cols-4 gap-3">
{data.restaurants.map((item, index) => (
<Link href={`/detail/${item.id}`} key={item.id}>
<RestaurantCard
image={item.pictureId}
name={item.name}
city={item.city}
key={item.id}
rating={item.rating}
/>
</Link>
))}
</div>
</>
);
}
but it always shows not found. i tried to use hooks but the result is the same. what am i missing?
Problem :
Expected Output :
Possible Cause:
Solution:
Folder Structure should be :
Here's a small example code :
page.js
atprojectName\src\app\page.js
:details page at
projectName\src\app\detail\[restaurantID]\page.js
:This way you can get desired output.
Please Read :