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.

contextType apparently not working in dev mode server side rendering

See original GitHub issue

Do you want to request a feature or report a bug? Bug

What is the current behavior? Crash during server render due to this.context being undefined. Bug only happens in SSR, and only in dev mode react. Issue appears neither in browser renders, nor in production mode server renders.

note: Using https://reactjs.net/ for c# server-side rendering, with bundled-in version of react disabled (so using the same version of react as the client bundle)

(simplified) application structure

// root component
const {Provider, Consumer} = createContext({});
const Root = props => return <Provider value={props.notNull}>{props.children}</Provider>;

// consumer
class ConsumingComponent extends Component {
  static contextType = Consumer;

  render() {
    if (this.context.someValue) return null;
    return <div/>;
  }
}

// render tree
<Root>...<ConsumingComponent/>...</Root>

// output of server render
JsRuntimeException: TypeError: unable to get property 'someValue' of undefined or null reference.

What is the expected behavior? this.context in the consuming component should be the value passed to the Provider, and not undefined.

Which versions of React, and which browser / OS are affected by this issue? Did this work in previous versions of React?

The bug, as best as I can pin down, appeared in 16.6.3. The code works in 16.6…1, and 16.6.2 is listed as a broken release in the changelog.

Issue Analytics

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

github_iconTop GitHub Comments

1reaction
awearycommented, Feb 11, 2019

@gaearon we currently warn only when passing Context.Provider, should we extend that to also warn for Context.Consumer?

1reaction
gaearoncommented, Feb 8, 2019

@hamlim Good catch.

Read more comments on GitHub >

github_iconTop Results From Across the Web

React Context not working with server side rendering
contextType only works for when the react version is >= 16.6.0 . This isn't mentioned in the react docs - https://reactjs.org/docs/context.html# ...
Read more >
React Server Side Rendering With Node And Express
Solutions for these issues exist (e.g. FastCGI and writing scripts in compiled language like C/C++) but are not ideal. More importantly, Perl ...
Read more >
10 Most Common Mistakes That PHP Developers Make - Toptal
Common Mistake #1: Leaving dangling array references after foreach loops ... The problem is that, if you're not careful, this can also have...
Read more >
Lightning Aura Components Developer Guide
that are composed of only markup can be rendered server-side, where $Resource isn't available. To avoid this, when you create a.
Read more >
Mura Rendering - Mura Docs v7.1
jsLibLoaded, boolean, false, This allows developers to not rely on Mura to load the ... Each of Mura's modules contain some server-side logic,...
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