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
+76
−0
Open
Changes from all commits
Commits
File filter...
Filter file types
Jump to…
Jump to file
Failed to load files.
| @@ -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. | ||
|
||
|
|
||
| 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) | ||
ljharb
Collaborator
|
||
|
|
||
| ```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> | ||
ljharb
Collaborator
|
||
| ``` | ||
|
|
||
| ## Class vs `React.createClass` vs stateless | ||
|
|
||
ProTip!
Use n and p to navigate between commits in a pull request.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.


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