X Tutup
The Wayback Machine - https://web.archive.org/web/20200529215357/https://github.com/airbnb/javascript/pull/1537/files
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

[guide] [react] HOC WrappedComponent static property #1537

Open
wants to merge 1 commit into
base: master
from
Open
Changes from all commits
Commits
File filter...
Filter file types
Jump to…
Jump to file
Failed to load files.

Always

Just for now

@@ -5,6 +5,7 @@
## Table of Contents

1. [Basic Rules](#basic-rules)
1. [High-Order Components](#high-ordered-components)
1. [Class vs `React.createClass` vs stateless](#class-vs-reactcreateclass-vs-stateless)
1. [Mixins](#mixins)
1. [Naming](#naming)
@@ -26,6 +27,81 @@
- However, multiple [Stateless, or Pure, Components](https://facebook.github.io/react/docs/reusable-components.html#stateless-functions) are allowed per file. eslint: [`react/no-multi-comp`](https://github.com/yannickcr/eslint-plugin-react/blob/master/docs/rules/no-multi-comp.md#ignorestateless).
- Always use JSX syntax.
- Do not use `React.createElement` unless you're initializing the app from a file that is not JSX.

## High-Order Components

- For [Higher-Order Components](https://facebook.github.io/react/docs/higher-order-components.html) define static property `WrappedComponent` as a reference to decorated component.

This comment has been minimized.

Copy link
@ljharb

ljharb Aug 25, 2017

Collaborator

This link doesn't suggest adding this static property. Where is this convention coming from?


If your High-Order Components don't provide access to decorated components, you can get faced with issue of [`import/no-named-as-default`](https://github.com/benmosher/eslint-plugin-import/blob/master/docs/rules/no-named-as-default.md)

This comment has been minimized.

Copy link
@ljharb

ljharb Aug 25, 2017

Collaborator

This isn't really a problem due to enzyme's .dive() method - "wrapped" components no longer need to be directly exported for the same of tests.


```jsx
// Title.jsx
import bindBreakpoint from './utils/bindBreakpoint';
// bad: using named export
export function Title({ breakpoint, children }) {
const sizeSmall = '16px';
const sizeRegular = '21px';
const style = {};
switch(breakpoint) {
case 'xn':
style.fontSize = sizeSmall;
break;
case 'n':
case 'm:
case 'w':
default:
style.fontSize = sizeRegular;
break;
}
return (
<h1 style={style}>{children}</h1>
);
}
export default bindBreakpoint(Title);
```

We are using export non-decorated `<Title />` component ONLY for testing purposes.

If we try to import `<Title />` throughout application in this way:

```jsx
import Title from './Title';
```

We definitely will get an error: [`import/no-named-as-default`](https://github.com/benmosher/eslint-plugin-import/blob/master/docs/rules/no-named-as-default.md)

```jsx
// utils/bindBreakpoint.jsx
export default function bindBreakpoint(WrappedComponent) {
function WithBreakpoint(props) {
return <WrappedComponent {...props} />;
}
WithBreakpoint.WrappedComponent = WrappedComponent;
return WithBreakpoint;
}
```

Now, we do have reference to the decorated component, so that, we can get rid of named export:

```jsx
// Title.jsx
import bindBreakpoint from './utils/bindBreakpoint';
// good: no named export
function Title({ breakpoint, children }) {
// impementation
}
export default bindBreakpoint(Title);
```

In our tests, we can get access to Pure, non-decorated `<Title />` component in this way:

```jsx
// Title.test.jsx
import Title from './Title';
shallow(<Title.WrappedComponent>Hello, World!</Title.WrappedComponent>

This comment has been minimized.

Copy link
@ljharb

ljharb Aug 25, 2017

Collaborator

specifically, shallow(<Title />).dive() is the proper way to do this.

```

## Class vs `React.createClass` vs stateless

ProTip! Use n and p to navigate between commits in a pull request.
You can’t perform that action at this time.
X Tutup