Context value undefined in server
See original GitHub issueHello,
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:
- Created 4 years ago
- Comments:6 (3 by maintainers)
@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
@vspedr so this is general setup problem. Try this:
src/Context.js
src/client.js
:src/Home.js
:You will see that even with regular React.Context server will print:
cc @contra