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.

Context value undefined in server

See original GitHub issue

Hello,

I’m trying to provide a value to view width on the initial server render in my SSR app. However I noticed that the context value is undefined in the server, then it gets updated to the value I provided once it gets rendered by the browser. This causes some of my components to not be rendered at first, but once the page rerenders in the browser they work just fine.

I’m using after.js for SSR and tried wrapping my outermost component with the Context provider when I noticed the issue. Later I did the following inside a component, just for investigation:

    <ResponsiveContext.Provider value={{ width: 100 }}>
      <ResponsiveContext.Consumer>
        {(values) => {
          console.warn({ values });
          return null;
        }}
      </ResponsiveContext.Consumer>
    </ResponsiveContext.Provider>

And the result was the same: server prints { values: undefined } and the browser prints {values: {…}}

react version is 16.8.6 react-responsive version is 7.0.0

Is this some unexpected behavior from this library or am I doing something wrong?

Issue Analytics

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

github_iconTop GitHub Comments

1reaction
vspedrcommented, Jun 14, 2019

@Tomekmularczyk thanks for the clarification, indeed it’s not a bug in react-responsive so I’ll close this issue. I’m still not sure why it wouldn’t work like that, but I found a workaround by wrapping each one of my route components with the ResponsiveContext provider.

For reference: https://github.com/jaredpalmer/after.js/issues/221

0reactions
Tomekmularczykcommented, Jun 14, 2019

@vspedr so this is general setup problem. Try this:

  1. Create src/Context.js
import React from "react";

export const TestContext = React.createContext();
  1. Do this in src/client.js:
...
import { TestContext } from "./Context";

ensureReady(routes).then(data =>
  hydrate(
      <TestContext.Provider value="test">
        <BrowserRouter>
          <After data={data} routes={routes} />
        </BrowserRouter>
      </TestContext.Provider>,
    document.getElementById("root")
  )
);
  1. and in src/Home.js:
import { TestContext } from "./Context";

class Home extends Component {
  ...

  render() {
    return (
      <div className="Home">
        <h1>Demo</h1>
        {/* A plain react context */}
        <TestContext.Consumer>
          {values => {
            console.warn({ values });
            return (
              <div>
                ...
              </div>
            );
          }}
        </TestContext.Consumer>

You will see that even with regular React.Context server will print:

{ values: undefined }

cc @contra

Read more comments on GitHub >

github_iconTop Results From Across the Web

context value is undefined on server with Webpack 5 #18090
I'm currently facing a similar issue after upgrading to webpack 5. In the first render in development everything seems to be fine, but ......
Read more >
NextJS: Context values undefined in production (works fine in ...
js application using React's Context api . Everything works fine during development, however, Context provider -related problems have arisen on ...
Read more >
Typing React Context to avoid an undefined default value
In this function we create a context with a generic type and undefined as the default value, we then create another function to...
Read more >
OSB Assign action failed, messageID: undefined dynamic ...
Hello team,. Environment: 1) Oracle Webloic Server (WLS) 12.2.1.4.0. 2) Oracle Service Bus (OSB).
Read more >
Context - React
Context provides a way to share values like these between components without having ... Note: passing undefined as a Provider value does not...
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