Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Context in react #64

Merged
merged 6 commits into from
Feb 13, 2017
Merged

Context in react #64

merged 6 commits into from
Feb 13, 2017

Conversation

jackfranklin
Copy link
Owner

No description provided.

In React the primary mechanism for communication between your components is through properties, or `props`, for short. Parent components can pass properties down to their children:

```js
const SomeParentComponent = () => {
Copy link
Contributor

Choose a reason for hiding this comment

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

I'd remove Some prefixes to ease readability.

The key thing about this communication is that it's _explicit_. Looking at the code above, you know how the components are communicating, where the `letMeKnowAboutSomeThing` function comes from, who calls it, and which two components are in communication. [You can see this in action on CodePen](http://codepen.io/jackfranklin/pen/vgvYOa?editors=0011).

This property of React, its explicitness of data passing between components, is one of its best features. React is very explicit as a rule, and this is in my experience leads to clearer code that's much easier to maintain and debug when something goes wrong. You simply have to follow the path of props to find the problem.

Copy link
Contributor

Choose a reason for hiding this comment

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

I would add a paragraph exposing the need for context:

One issue you might find in big apps is that you might need to pass props from a top level ParentComponent to really a deeply nested ChildComponent. The components in between will probably have no use the these props and should probably not even know about them.


This property of React, its explicitness of data passing between components, is one of its best features. React is very explicit as a rule, and this is in my experience leads to clearer code that's much easier to maintain and debug when something goes wrong. You simply have to follow the path of props to find the problem.

As you might have guessed, context is a way to break this explicitness. When a component defines some data onto its _context_, any of its descendants can access that data. That means any child further down in the component tree can access data from it, without being passed it as a property. Let's take a look at context in action.
Copy link
Contributor

Choose a reason for hiding this comment

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

I like to think of context as a props portal between a Parent and a Child. hat do you think about using this wording to describe what context is?

Copy link
Contributor

Choose a reason for hiding this comment

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

Along with an illustration like:

screen shot 2017-02-13 at 13 26 35

Source sketch

Copy link
Owner Author

Choose a reason for hiding this comment

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

Ooo I like the use of the word portal. That's perfect!


As you might have guessed, context is a way to break this explicitness. When a component defines some data onto its _context_, any of its descendants can access that data. That means any child further down in the component tree can access data from it, without being passed it as a property. Let's take a look at context in action.

First, on the _parent component_, we define two things:
Copy link
Contributor

Choose a reason for hiding this comment

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

I'd insert a title here like

How to use context?

@ArnaudRinquin
Copy link
Contributor

The rest of the article (when to (not) use) is :shipit: for me!

@jackfranklin
Copy link
Owner Author

@ArnaudRinquin thank you! I've added some diagrams & updated based on your suggestions.


## When to use context

If you're a library author, context is useful. Libraries like [React Router use context](https://github.com/ReactTraining/react-router/blob/v4/packages/react-router/modules/Router.js#L13) to allow the components that they provide application developers to communicate. When you're writing a library that provides components that need to talk to each other, or pass values around, `context` is perfect.
Copy link
Contributor

@ArnaudRinquin ArnaudRinquin Feb 13, 2017

Choose a reason for hiding this comment

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

I just though about it: another famous context based library is react-redux. It'd be nice to cite it as an example or even base your analogy on it as it seems easier to understand compared to react-router.


One issue you might find in big apps is that you might need to pass props from a top level `ParentComponent` to a deeply nested `ChildComponent`. The components in between will probably have no use the these props and should probably not even know about them. When this situation arises, you can consider using React's context feature.

Context acts like a portal in your application in which components can place data that can be accessed without being passed through explictly.
Copy link
Contributor

@ArnaudRinquin ArnaudRinquin Feb 13, 2017

Choose a reason for hiding this comment

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

I'd avoid saying place data as it can be data but also callbacks props. I would just use props

@jackfranklin jackfranklin merged commit 8bda00f into gh-pages Feb 13, 2017
@jackfranklin jackfranklin deleted the context-in-react branch February 13, 2017 15:58
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants