Skip to content

Instantly share code, notes, and snippets.

@sand97
Created January 7, 2022 08:35
Show Gist options
  • Save sand97/654533dba88eefb7cc861d8607729c40 to your computer and use it in GitHub Desktop.
Save sand97/654533dba88eefb7cc861d8607729c40 to your computer and use it in GitHub Desktop.
nextjs get server side props example
const Product = (props) => {
return <div>
{ props.data.map(p => <div key={p.id}>{p.name}</div>) }
</div>
}
export const getServerSideProps = async (context) => {
const {name} = context.query
//you can fetch data from your api like this
// const res = await
// fetch(`api.yourcms.com/product?keyword=${name}`);
return {
props: {data: [{name: 'Banana', id: 1},
{name: 'Potato', id: 2}]
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment