question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Page rendering uses wrong useState default value

See original GitHub issue

Description

In production, page rendering is partially wrong when using useState with a default value that depends on window.

Steps to reproduce

The following fork of a starter template demonstrates the bug. Check specifically index.js and index.css. It boils down to a useState getting initialised with this piece of code:

if ((typeof window) === 'undefined') { // required for build not to fail
    return NO_URL;
}

return window.location.href;

The conditional statement is there for gatsby to be able to build, because window is not defined during building.

Expected result

The following is obtained with yarn start / gatsby develop. All is well.

image

Actual result

This is obtained when building with rm -rf .cache public && ./node_modules/.bin/gatsby build --prefix-paths

image

The red text is because of a CSS class that would only be set if the initial value of the setState were NO_URL. But the shown url is obviously not NO_URL. So the colour and the text contradict each other.

Environment

  System:
    OS: Linux 5.3 Ubuntu 19.10 (Eoan Ermine)
    CPU: (8) x64 Intel(R) Core(TM) i5-8265U CPU @ 1.60GHz
    Shell: 5.0.3 - /bin/bash
  Binaries:
    Node: 12.16.1 - /tmp/yarn--1590166014711-0.6080215808640919/node
    Yarn: 1.21.1 - /tmp/yarn--1590166014711-0.6080215808640919/yarn
    npm: 6.13.4 - ~/.nvm/versions/node/v12.16.1/bin/npm
  Languages:
    Python: 2.7.17 - /usr/bin/python
  Browsers:
    Chrome: 81.0.4044.138
    Firefox: 76.0.1
  npmPackages:
    gatsby: ^2.21.37 => 2.21.37

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:8 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
NeroVanbiervlietcommented, May 23, 2020

All clear, thanks a lot for your help!

1reaction
herecydevcommented, May 22, 2020

You’ve identified the reason correctly: that window isn’t available during gatsby build process. If you’re after information about the location there is a prop injected into each page that provides this.

Read more comments on GitHub >

github_iconTop Results From Across the Web

React - useState not setting initial value - Stack Overflow
Show activity on this post. I have a basic functional component like so: export const MakeComponent = props => { const { path,...
Read more >
How to Fix the React useState Hook Not Setting Initial Value ...
The useState hook lets us create state variables in our React components. It takes an argument for the initial value of the state....
Read more >
My useState always renders the default value when ... - Reddit
The default value of the UseState variable is false . It's set to true by a click event so that the component loads....
Read more >
Do not use props as default value of React.useState() directly
Once we defined React. useState, the default value will never change, because useState only called once, even the num props changed, the state ......
Read more >
5 Most Common useState Mistakes React Developers Often ...
A typical solution to this error and UI not rendering is using conditional checks to validate the state's existence to check if it...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found