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.

Limitations of React.createContext

See original GitHub issue

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

feature

What is the current behavior?

The current behavior requires end users to use createContext in the module scope. To my understanding, it’s not currently possible to use a default value derived from the state of a component (a stateful Provider in my case).

This StackOverflow post hits the issue right on IMO.

I feel like this is the classic use case for replacing Redux, and it doesn’t work out of the box with static types.

I think it’s quite telling that react-redux is doing something similar here in their PR to move to React 16 context. I would expect the default value to be this.state of the Provider component instead of null.

My knowledge of React internals is naive, but I didn’t see anyone else bringing up this issue.

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:

What is the desired behavior?

Maybe a JSX API for context creation? I imagine it’s not quite that simple.

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

16.3+

Issue Analytics

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

github_iconTop GitHub Comments

1reaction
jfo84commented, Oct 5, 2018

@jquense Flow fails when you consume the context AFAIK

1reaction
jquensecommented, Oct 5, 2018

It is optional? React.createContext() works just fine i believe

Read more comments on GitHub >

github_iconTop Results From Across the Web

Pitfalls of overusing React Context - LogRocket Blog
React Context is an excellent API for simple apps with infrequent state changes, but it can quickly devolve into a developer's nightmare if...
Read more >
The Problem with React's Context API | Strings and Things
The problem with context is simple: Everything that consumes a context re-renders everytime that context's state changes. That means that if you ...
Read more >
Comparing React Context and Redux
While Context with hooks can technically substitute Redux, it introduces a number of limitations regarding application performance and maintainability.
Read more >
Why I never use React.useContext - Julian​Garamendy​.dev
Our banana will be undefined if our component doesn't have a BananaContext.Provider up in the tree. This has some drawbacks: Our component needs ......
Read more >
React Context vs Redux: Which one is the right winner
We focus on the advantages and disadvantages of React Context and Redux ... Only little setup is required - create context and sometimes...
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