getServerSideProps never gets called #12758
-
here's a fragment from my "next": "^9.3.6", I have a file: export async function getServerSideProps() {
console.log('getServerSideProps()')
return { props: {data: 'test'} }
}
export default ({data}) => (
<p>
data: {data}
</p>
) however, when I run it, I never get any output on the console and I get no data. what am I doing wrong? Appendix I as a second attempt at this I've tried: import React from 'react'
export default class Tst extends React.Component {
getInitialProps() {
console.log('getInitialProps')
var props = { data: 'test' }
return { props }
}
render({data}) {
return (<p>{data}</p>)
}
} but that fails to run, complaining on the line
gosh. now what? Appendix II this doesn't puke but it also doesn't display any data: render() {
return (<p>{this.props.data}</p>)
} |
Beta Was this translation helpful? Give feedback.
Replies: 7 comments 19 replies
-
I tested your initial piece of code on my test setup. It works fine, so the problem is probably somewhere else. Thus, sharing how you have set things up, or other parts of your project will be much more helpful. Such that this behavior is reproducible. You can also try reproducing this behavior in a test setup on your own. This might give you more insight into where the actual problem is coming from. |
Beta Was this translation helpful? Give feedback.
-
well... the examples show return { props: {data: 'test} } but I'm finding that's wrong. if the function returns: return {data: 'test'} I can pick it up with
what else do I need to do? |
Beta Was this translation helpful? Give feedback.
-
if I do this: render(props) {
return (
<div>
[{JSON.stringify(props)}]
</div>
)
} I see |
Beta Was this translation helpful? Give feedback.
-
ok. I think there's something screwy with the class declaration. this seems to work: export async function getServerSideProps() {
return {props: {data: 'test'}}
}
export default ({data}) => (
<div>
data: [{data}]
</div>
) |
Beta Was this translation helpful? Give feedback.
-
one other thing weird. my export default () => (
<Tst />
) and if I call this is maddening! |
Beta Was this translation helpful? Give feedback.
-
if I call the component like this: export default () => (
<Tst bloody="hell" />
) I find that if I call my index page I see I'm starting to regret having picked NextJs for this project. WTF?? |
Beta Was this translation helpful? Give feedback.
-
Hey there! I will try to address each comment individually, let me know if you still have questions! The first snippet you sent is correct, saving the file as The class snippet with static getInitialProps() {
console.log("getInitialProps");
var props = { data: "test" };
return { props };
} You can also remove this The second issue with the snippet is with the render() {
return <p>{this.props.data}</p>;
} This is the fixed code of the page: import React from "react";
export default class Tst extends React.Component {
static getInitialProps() {
console.log("getInitialProps");
return { data: "test" };
}
render() {
return <p>{this.props.data}</p>;
}
} There are some differences between
|
Beta Was this translation helpful? Give feedback.
Hey there! I will try to address each comment individually, let me know if you still have questions!
The first snippet you sent is correct, saving the file as
pages/tst.js
and accessing the page in http://localhost:3000/tst will return the correct result in the browser (data: test
) and loggetServerSideProps()
in the terminal that you started the server.The class snippet with
getInitialProps
doesn't work because it is defining the function as a instance method, it must be a static method in order to be accessed likeYourPage.getInitialProps
(notice that we don't need to create an instance of the class to use the method). If you add thestatic
keyword it will start logginggetInitialProps
…