Skip to content
This repository has been archived by the owner on Apr 9, 2020. It is now read-only.

Support Functional Components #85

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
a716835
Add mocha env to .eslintrc to fix editor UI errors
ericclemmons Feb 16, 2016
07d0d44
Comments in actual.js files are passed in as `it(should, ...)`
ericclemmons Feb 16, 2016
acc0fa2
Transform pure functional Components into Classes prior to wrapping
ericclemmons Feb 16, 2016
9bdc44a
Support functional default export
ericclemmons Feb 16, 2016
94fd110
Support named export
ericclemmons Feb 16, 2016
9b438ee
Functional argument names are used as render() variables
ericclemmons Feb 17, 2016
3b4da54
Support internal top-level Component variables & functions
ericclemmons Feb 17, 2016
268e2b7
Add test for ignoring unnamed functions
ericclemmons Feb 17, 2016
120fb0c
Clarify test assertions
ericclemmons Feb 17, 2016
a93a82e
Remove test filter
ericclemmons Feb 17, 2016
3ab32f9
Add test for ignoring nested functions
ericclemmons Feb 17, 2016
2fd286d
Remove redundant test
ericclemmons Feb 17, 2016
a05cc53
Explicitly use Component.id
ericclemmons Feb 17, 2016
3fe2d2e
Functional Components explicitly use React.Component as superClass
ericclemmons Feb 17, 2016
d827bed
React is added if not in scope.
ericclemmons Feb 17, 2016
6d68934
Ensure only functional VariableDeclarations are processed
ericclemmons Feb 17, 2016
7073556
Update README with "transformReactLikeFunctionsToClasses" option
ericclemmons Feb 17, 2016
de055c4
Default transformReactLikeFunctionsToClasses to false
ericclemmons Feb 17, 2016
a162a3e
Only transform functions when transformReactLikeFunctionsToClasses is…
ericclemmons Feb 17, 2016
774cb8c
Add test for default value of transformReactLikeFunctionsToClasses
ericclemmons Feb 17, 2016
3cececc
Update functional component tests to use "transformReactLikeFunctions…
ericclemmons Feb 17, 2016
fb1a1e9
Replace absolute path with %FIXTURE_PATH%
ericclemmons Feb 17, 2016
3781b40
Configuration deserves a top-level entry in the ToC
ericclemmons Feb 17, 2016
29c847f
Improve explanation of functional components
ericclemmons Feb 17, 2016
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
"semi": 1
},
env: {
"mocha": true,
"node": true
}
}
53 changes: 50 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ This plugin wraps React components with arbitrary transforms. In other words, **
* [Ecosystem](#ecosystem)
* [Demo Project](#demo-project)
* [Installation](#installation)
* [Configuration](#configuration)
* [Writing Transforms](#writing-transforms)

## Ecosystem
Expand Down Expand Up @@ -54,7 +55,7 @@ npm install --save-dev react-transform-hmr
npm install --save-dev react-transform-catch-errors
```

##### Configuration
## Configuration
Add react-transform to the list of plugins in your babel configuration (usually `.babelrc`):

```js
Expand Down Expand Up @@ -84,10 +85,16 @@ Add react-transform to the list of plugins in your babel configuration (usually
}, {
// can be an NPM module name or a local path
"transform": "./src/my-custom-transform"
}]
}],

// by default we only look for `React.createClass` (and ES6 classes)
// but you can tell the plugin to look for different component factories:
// factoryMethods: ["React.createClass", "createClass"]
// "factoryMethods": ["React.createClass", "createClass"]

// Disabled by default, if you want to enable transforms
// (e.g. HMR) on stateless functional components, this will
// convert them into React.Component classes for you.
"transformReactLikeFunctionsToClasses": false
}]
]
}
Expand All @@ -101,6 +108,46 @@ Note that when using `React.createClass()` and allowing `babel` to extract the `

You may optionally specify an array of strings called `factoryMethods` if you want the plugin to look for components created with a factory method other than `React.createClass`. Note that you don’t have to do anything special to look for ES6 components—`factoryMethods` is only relevant if you use factory methods akin to `React.createClass`.

### Stateless Functional Components

If you enable transforming
[stateless functional components](https://facebook.github.io/react/blog/2015/10/07/react-v0.14.html#stateless-functional-components)
into `React.Component` classes via:

```json
"transformReactLikeFunctionsToClasses": true`
```

The preferred, most reliable format looks like:

```js
const Greeting = ({ name }) => (
<h1>Howdy there, {name}!</h1>
);

export default Greeting;
```

Or, if you prefer a single statement, like this:

```js
export default function Greeting({ name }) {
return (
<h1>Howdy there, {name}!</h1>
);
}
```

This satisfies the **React-like _signatures_** to infer this is
indeed a React component:

- [x] `const` or `export`ed function.
- [x] Capitalized function/variable name.
- [x] Function body `return`s JSX.

As long as these conventions are followed, these functional components
should be successfully converted into classes for further transformation.

## Writing Transforms

It’s not hard to write a custom transform! First, make sure you call your NPM package `react-transform-*` so we have uniform naming across the transforms. The only thing you should export from your transform module is a function.
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
"babel-core": "^6.2.1",
"babel-eslint": "^4.1.6",
"babel-preset-es2015": "^6.1.18",
"babel-preset-react": "^6.5.0",
"babel-register": "^6.2.0",
"eslint": "^1.10.3",
"eslint-plugin-react": "^3.11.2",
Expand Down
Loading