How to solve Your `getStaticProps` function did not return an object in Next.js

Cover Image for How to solve Your `getStaticProps` function did not return an object in Next.js
Ali Bentaleb

In Next.js, static pages can use getStaticProps function which must return an object.

This object should have at least one of the keys which are: revalidate, props,redirect or notFound.

If no return object found for getStaticProps function, the Error Your getStaticProps function did not return an object. Did you forget to add a return? is thrown.

To resolve it, you must add in a return object like this

export const getStaticProps = async () => {
	//must return an object

	return {
		props: {
			name: 'Ali'
		}
	};
};
In this example, we return a props object and we can put whatever object we like to be returned.

Our page then can use these props to render its content.

How to use getStaticProps

In static generation page props are returned via getStaticProps.

The function is asynchronous and should be exported on the page where it is used

The function must return an object with key props, and then you can put whatever values you wish to return, either from an API or any external source.

Let’s see an example of how to use it correctly

export default function Page(props) {
	return (
		<div>
			your name is {props.name} and your id is {props.id}
		</div>
	);
}

export const getStaticProps = async () => {
	return {
		props: {id: 1, name: 'Ali'}
	};
};

The default export is necessary so that the page could render something, and the getStaticProps provide whatever props are needed for the page to render.

If you are interested more about understanding getStaticProps and how it works in details, you can check my full article on [this link.][1]

Conclusion

We have seen how to solve `getStaticProps` function did not return an object, if you would love to learn more about Next.js, check [this link][2]
How to solve Additional keys w...How to build login form with f...