I'm newish to Next.js and the rest of React and was using it (and Tailwind CSS) on a project. I was working with getStaticProps to grab data from a json file and display different sections of it on different pages using dynamic routes. I have much of the project working, but I can't get it to get the data piped all the way through from the json file to the frontend of the final webpage. My theory is that the problem lies in getStaticProps, although of course I could be wrong.
I have the following /app/projects/[fname]/page.js:
import allProjects from '../../projectData.json';
function getProjectData({projectName}) {
let projectData = null;
for (const entry of allProjects) {
if (entry.fname === projectName) {
projectData = entry;
break;
}
}
return projectData;
}
export async function getStaticPaths() {
const paths = allProjects.map(project => {
return {
params: {
fname: project.fname
}
}
});
return {
paths: paths,
fallback: false
}
}
export async function getStaticProps(context) {
const {params} = context;
let data = getProjectData(params.fname);
return {
props: {
title: data.title,
desc: data.desc,
img: data.img,
github: data.github,
link: data.link,
}
}
}
export default function Page({title, desc, img, github, link}) {
return (
<div className='grid grid-cols-6 m-1'>
<ul className='m-4'>
<li>Title: {title}</li>
<li>Desc: {desc}</li>
<li>Img: {img}</li>
<li>GitHub: {github}</li>
<li>Link: {link}</li>
</ul>
</div>
);
}
And the following /app/project.json:
[
{
"title": "thing0",
"fname": "thing0",
"desc": "hi there this the description",
"img": "here image path",
"github": "here github url",
"link": "here other link if available"
},
{
"title": "thing1",
"fname": "thing1",
"desc": "hi there this the description",
"img": "here image path",
"github": "here github url",
"link": "here other link if available"
},
{
"title": "thing2",
"fname": "thing2",
"desc": "hi there this the description",
"img": "here image path",
"github": "here github url",
"link": "here other link if available"
}
]
This is the result I get. As you can see, it seems to count each one as null or something.
Any thoughts? Thank you!