Skip to content

Context doc#7954

Merged
lacker merged 2 commits intofacebook:new-docsfrom
lacker:context
Oct 13, 2016
Merged

Context doc#7954
lacker merged 2 commits intofacebook:new-docsfrom
lacker:context

Conversation

@lacker
Copy link
Copy Markdown
Contributor

@lacker lacker commented Oct 12, 2016

The content is mostly similar to how it was before. I added a mention of Redux and MobX, because in several of the situations described by this document, people should really consider using one of those libraries before they start messing with context themselves.

@lacker
Copy link
Copy Markdown
Contributor Author

lacker commented Oct 12, 2016

screencapture-localhost-4000-react-docs-context-html-1476298768715

Comment thread docs/docs/context.md Outdated
> Context is an advanced and experimental feature. The API is likely to change in future releases.
>
> Most applications will never need to use context. Especially if you are just getting started with React, you likely do not want to use context. Using context will make your code harder to understand because it makes the data flow less clear. It is similar to using global variables to pass state through your application.
> Context is an advanced and experimental feature. If you are not an experienced React developer, you do not want to use context. If you are getting annoyed by passing props down manually through many levels of a tree, first try using [Redux](https://github.com/reactjs/redux) or [MobX](https://github.com/mobxjs/mobx).
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This makes it seem like these libraries offer an abstraction on top of context. However they don't: libraries you link to are independent of React.

There are React bindings for these libraries which happen to use context, but they are called differently (React Redux, mobx-react) and are in separate repos.

This paragraph also makes it seem like passing data down is the primary responsibility of these libraries. But both Redux and MobX are much more opinionated about state handling in general.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK - i basically redid this part

Comment thread docs/docs/context.md Outdated
## Known limitations
## Known Limitations

If a context value provided by a component changes, descendants that use that value won't update if an intermediate parent returns `false` from `shouldComponentUpdate`. See issue [#2517](https://github.com/facebook/react/issues/2517) for more details.
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In general I feel like not enough time is spent explaining that this is an experimental API and it is likely to break in the future. Unlike props it is likely you'll have to change every single component that he uses context when we change its API.

Also even though you describe how updates work they are just broken. The limitation in the last sentence is very severe because it is completely out of control of the components using context. So essentially you can consider updates to context broken, abd we should recommend treating it as unchanging because it's the only way to use it safely. You can still pass your own subscription system through it.

This is a really good article about it. I'm not sold on its conclusion ("wrap everything with MobX") because MobX wraps all native data structures into its own, and has its share of issues related to that, but the rest of the article highlights pitfalls of context very well:

https://medium.com/@mweststrate/how-to-safely-use-react-context-b7e343eff076

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK, I added a bunch more about "why not to use context". I also just added a link to that article.

@lacker
Copy link
Copy Markdown
Contributor Author

lacker commented Oct 13, 2016

How's this?

@gaearon
Copy link
Copy Markdown
Collaborator

gaearon commented Oct 13, 2016

LGTM

@lacker lacker merged commit f4854f6 into facebook:new-docs Oct 13, 2016
@lacker lacker deleted the context branch October 13, 2016 23:39
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants