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.

unexpected ContextConsumer or ContextProvider behavior

See original GitHub issue

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

What is the current behavior? When use two or more contexts that created by createContext,in the child-tree of cxt2.Provider,i use cxt1.Provider to set a new context of cxt1,i expect only the child consumer got the new cxt1,but the result is that the cxt2.Provider’s next sibling’s child-tree got an unexpected cxt2.Provider’s new context

If the current behavior is a bug, please provide the steps to reproduce and if possible a minimal demo of the problem. Your bug will get fixed much faster if we can run your code and it doesn’t have dependencies other than React. Paste the link to your JSFiddle (https://jsfiddle.net/Luktwrdm/) or CodeSandbox (https://codesandbox.io/s/new) example below: createContext issue

What is the expected behavior? ContextProvider only affect the children node.

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

Issue Analytics

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

github_iconTop GitHub Comments

2reactions
bvaughncommented, Feb 8, 2018

Yeah, that’s what we were thinking too. Just had to step away for meetings right after writing the test 😅 Thanks Brandon!

1reaction
awearycommented, Feb 8, 2018

@bvaughn I took a look, and from what I can tell the problem is related to popProvider:

https://github.com/facebook/react/blob/cbf729659e784792c7b57e8e8a0e88ed6359c604/packages/react-reconciler/src/ReactFiberNewContext.js#L53-L55

previousProviderFiber ends up being the wrong provider (UidContext2), so when context.currentValue is updated to match previousProviderFiber.pendingProps.value it’s being updated from the wrong provider.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Context works strange in 16.6.3 in renderToStaticMarkup
In version react-dom@16.6.1 I used Context.Consumer in renderToStaticMarkup and it works fine if a call it, ... bug or unexpected behavior.
Read more >
Context - React
Context.Provider ... Every Context object comes with a Provider React component that allows consuming components to subscribe to context changes. The Provider ...
Read more >
A (Mostly) Complete Guide to React Rendering Behavior
React checks to see if a context provider has been given a new value when the surrounding component renders the provider. If the...
Read more >
When does React render your component? - Zhenghao
Context consumers get rendered whenever the provider get rendered #. Passive rendering can also happen when your component is a context consumer.
Read more >
Context.Consumer vs useContext() to access values passed ...
I think useContext will subscribe to the context Provider, since we passed the Context as an argument, so that it will trigger a...
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