๐ฝ๏ธ Next.js Fetching methods - getStaticProps explained
Your data is ready to use!

I am a full stack developer, YouTuber and blogger!
Search for a command to run...
Your data is ready to use!

I am a full stack developer, YouTuber and blogger!
No comments yet. Be the first to comment.
## Hey all! This series will be all about using the different ways of fetching data in Next.js. Ways like **SSG**, **SSR**, and **ISR**
Hey all ๐๐ Webhooks are a powerful tool that allow applications to communicate with each other in real-time. They are often used to send notifications or updates from one system to another without requiring manual intervention. If you know the bas...

Hey everyone! It's been a while since I wrote my last article. But here I am with another topic that might be informational to you ๐. This time I am writing about why you should avoid the onclick attribute in your HTML and JavaScript code. This ar...

โ What and Why Memoriez โ This is a very important question because it makes things clear as to why this app is needed. I built Memoriez because I honestly found it hard to write and maintain journal/diary entries daily. It's not easy to maintain a j...

Explained with a simple project

๐ Hey developers! This post is about how you can sort import statements in your JavaScript/TypeScript/React/Node etc projects easily with Prettier in VS Code when you format the code. https://www.youtube.com/watch?v=QQWgN0_gUxI What will you achieve...

Today I thought of starting my first series about Next.js Fetching methods because many people (including me) don't understand them easily.
I will make these methods very easy for you to understand so that you don't have to be worried about how it works.

getStaticProps?In simple words, getStaticProps is a method that can be exported from a page in Next.js which is simply used to get some data for further use.
For eg.
export default function MainPage(props) {
return (
<h1>{props.helloWorld}</h1>
);
}
export const getStaticProps = () => {
return {
props: {
helloWorld: 'Hello World'
}
}
}
We get back data in the form of props, but we don't use getStaticProps this way ๐.
If you have a database or you want to fetch some data from an API, you can use getStaticProps. But only if that data is STATIC.
The getStaticProps method runs at the build time, so whatever you write inside the method, is available to you statically. And it does not change over time because YOU NEVER FETCH THE DATA AGAIN.

Now let's take a better example to use getStaticProps in Next.js and why you would want to use it instead of the normal fetching in React.
By using
getStaticProps, the data is only fetched once and is READY TO USE without refetching.
Suppose you are building an app that has a list of people, and that list is always static. You want to show or use that list but you have them stored in the database or you are using an external API.
We will take an example of JSONPlaceholder API and we want to fetch the users from the API.
This is how the code will look like:
export default function MainPage({ people }) {
return (
<div>
{people.map(person => (
<>
<h1>{person.name}</h1>
<h2>Website: {person.website}</h2>
Email: <code>{person.email}</code>
</>
))}
</div>
);
};
export const getStaticProps = async () => {
const response = await fetch('https://jsonplaceholder.typicode.com/users');
const people = await response.json();
return {
props: {
people
}
};
};
Now when the app gets built using npm run build, the data will be fetched at that point and it will be READY TO USE in your app.
So there is no need to fetch the data, show loading spinner, etc, etc. THE DATA IS ONLY FETCHED ONCE.

Thanks for reading!
I hope you liked it! Read the other posts of this series! Comment down your thoughts! There is always room for improvement so let me know your suggestions!
Connect with me on my YouTube channel and my Twitter ๐
Until next time, keeping awesome โ๏ธ!