Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

getInitialProps called twice #1031

Closed
fat opened this issue Feb 8, 2017 · 5 comments
Closed

getInitialProps called twice #1031

fat opened this issue Feb 8, 2017 · 5 comments

Comments

@fat
Copy link

fat commented Feb 8, 2017

Hi,

I have the same issue as #524

Here's a bit more info if it's helpful…

I have a super basic express server with the following routes:

  server.get('/u/:username', (req, res) => {
    User.getByUsername(req.params.username, (err, user) => {
      if (err) …do error stuff…
      return app.render(req, res, '/profile', {
        user: user
      })
    })
  })

  server.get('*', (req, res) => {
    return handle(req, res)
  })

and then the following "profile" page component:

export default class extends React.Component {
  static getInitialProps ({ query: { user } }) {
    return { user }
  }

  render () {
    return ()
  }
}

If i fetch /u/guille my server is correctly fetching the guillermo object and passing it down to my react component on first render…

However! once the javascript on the client loads - it is reinitializing the react component and calling getInitialProps a second time (this time with an empty user object, effectively wiping away all my data 😓).

Am I doing something wrong? Im a scrub so very possible.

@nkzawa
Copy link
Contributor

nkzawa commented Feb 8, 2017

@fat Is it possible to prepare a repository we can reproduce the issue ?

@fat
Copy link
Author

fat commented Feb 8, 2017

hey @nkzawa yep!

Here's a github repo: https://github.com/fat/next.js-bug

seems to be somethign to do with nested data…

for example if the query data i pass is:

{ id: 'foo' } it's great.

if i pass { user: { id: 'foo' } } it causes the client to refresh and lose the initial render.

(thanks for taking a look! <3)

@fat
Copy link
Author

fat commented Feb 8, 2017

ok… i think i was just misunderstanding how getIntitialProps is supposed to work…

i think im supposed to instead just pass the query string there of "username"… and then put the actual "fetch" of the data object in the getInitialProps method… which is my bad. (rather then what im doing, which is trying to fetch the data, then pass it down to get initial props).

@nkzawa
Copy link
Contributor

nkzawa commented Feb 8, 2017

@fat thank you, I can reproduce now.

I don't know why it occurs yet, but we don't support { user: { id: 'foo' } } since this is invalid as a parsed query string. It has to be { id: 'foo' }.

@fat
Copy link
Author

fat commented Feb 8, 2017

yep makes sense

@fat fat closed this as completed Feb 8, 2017
@lock lock bot locked as resolved and limited conversation to collaborators May 12, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants