Error With getServerSideProps
Unanswered
New Zealand Heading Dog posted this in #help-forum
New Zealand Heading DogOP
why when i use
export async function getServerSideProps(context) {
return fetchUser(context)
}
in some files it work but in compoent i get undefined
export async function getServerSideProps(context) {
return fetchUser(context)
}
in some files it work but in compoent i get undefined
12 Replies
Eulophid wasp
getServerSideProps can only be used in pages.
@Eulophid wasp getServerSideProps can only be used in pages.
New Zealand Heading DogOP
so if i want to fetch data to diplay it in the navbar how can i do that
Eulophid wasp
You would pass the data from the page on to the component where you need it using props:
index.js:
NavBar.js:
index.js:
export const getServerSideProps = async () => { ... }
export default function Page(props) {
return (
<NavBar navBarData={props.navBarData>
)
}NavBar.js:
export default function NavBar({navBarData}) {
return (
<div>{navBarData.title}</div>
)
}@Eulophid wasp You would pass the data from the page on to the component where you need it using props:
index.js:
javascript
export const getServerSideProps = async () => { ... }
export default function Page(props) {
return (
<NavBar navBarData={props.navBarData>
)
}
NavBar.js:
javascript
export default function NavBar({navBarData}) {
return (
<div>{navBarData.title}</div>
)
}
New Zealand Heading DogOP
I will try thanks 😊
New Zealand Heading DogOP
@Eulophid wasp it works but i want the navbar to be accessible on all pages not only the index
so i need to fetch the data on every page ?
Eulophid wasp
Put the navbar in _app.js in that case as it is part of your base layout
@Eulophid wasp Put the navbar in _app.js in that case as it is part of your base layout
New Zealand Heading DogOP
when i put it in the _app i got undefined also
Eulophid wasp
Ah yeah my bad you can't do that out of the box
@Eulophid wasp Ah yeah my bad you can't do that out of the box
New Zealand Heading DogOP
so i must fetch the data on every page ?
Asian black bear
yes, you have to fetch on every page. It is a major shortcoming of the /pages router
this can be mitigated by using ISR, but you are out of luck if the page also needs more dynamic data