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.

React Context Wrong Consumer Value

See original GitHub issue

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

What is the current behavior? Context provider contains the correct value but in the consumer I have the default value :

(value = bddf, the correct) image

(value = cdn, wrong value) image

Steps to reproduce I want to make a library that dynamically load the theme. I used create context.

I create a simple API for this : https://github.com/helabenkhalfallah/react-sg-components/tree/master/packages/sg-ui-core/src

I imported this lib as dependencies on my components : https://github.com/helabenkhalfallah/react-sg-components/blob/master/packages/sg-button/src/lib/SGButton.jsx#L3

And inside storybook : https://github.com/helabenkhalfallah/react-sg-components/blob/master/packages/storybook/stories/sg-button/SGButtonStory.jsx#L3

However inside context I get always the default value. When I inspect inside the provider I have the correct value but inside consumer I have the default value.

I tried also hooks but the same issue.

The idea I don’t want to pass to my components library the current ‘theme’ but each time the application started, it sets the context and components automatically consume that value.

What is the expected behavior? Consumer components should have the same value as the provider.

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

Is this because I create an npm module for ThemeContext ? I should have an npm package ThemeContext because many apps will consume it 😦

Thanks.

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:6

github_iconTop GitHub Comments

2reactions
bvaughncommented, Dec 16, 2019

Thanks for the update 👍

0reactions
helabenkhalfallahcommented, Dec 16, 2019

I found why, this is due to storybook, I get everything works perfectly without storybook, I created a separated project and import all dependencies. Thank you so much for your help @bvaughn 👍 😃 https://codesandbox.io/s/funny-pascal-w950v

Read more comments on GitHub >

github_iconTop Results From Across the Web

React Context doesn't show value in Consumer - Stack Overflow
I'm trying to pass value from Context Provider to Consumer, but it shows nothing. I've watched many questions on StackOverflow about React ...
Read more >
The child node of a React context consumer ... - DeepScan
If an incorrect value is passed or multiple children are specified, React outputs a warning message and a TypeError exception is thrown.
Read more >
Context - React
Context lets us pass a value deep into the component tree// without explicitly threading it through every component.// Create a context for the...
Read more >
react-safe-context - npm
Create React context objects that have built-in safety against having undefined value. Latest version: 1.0.8, last published: 6 months ago.
Read more >
Everything You Need to Know about React Context in 2022 ...
If no provider exists above the consumer, you will get the default value as defined when the context we created. Let's illustrate all...
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