1 | # babel-plugin-react-require
|
2 |
|
3 | Babel plugin that adds React import declaration if file contains JSX tags.
|
4 |
|
5 | This plugin is only about stateless components that doesn't extends `React.Component`.
|
6 | If you want to use any other React functions then you should import their by yourself.
|
7 |
|
8 | ## Example
|
9 |
|
10 | Your `component.js` that contains this code:
|
11 |
|
12 | ```js
|
13 | export default function Component() {
|
14 | return (
|
15 | <div />
|
16 | );
|
17 | }
|
18 | ```
|
19 |
|
20 | will be transpiled into something like this:
|
21 |
|
22 | ```js
|
23 | import React from 'react';
|
24 |
|
25 | export default function Component() {
|
26 | /* this part will be transpiled by babel itself as usual */
|
27 | return (
|
28 | React.createElement('div')
|
29 | );
|
30 | }
|
31 | ```
|
32 |
|
33 | ## Usage
|
34 |
|
35 | * Install `babel-plugin-react-require`.
|
36 |
|
37 | ```
|
38 | npm install babel-plugin-react-require --save-dev
|
39 | ```
|
40 |
|
41 | * Add `react-require` into `.babelrc`. This plugin should be defined before `transform-es2015-modules-commonjs` plugin because it's using ES2015 modules syntax to import `React` into scope.
|
42 |
|
43 | ```json
|
44 | {
|
45 | "plugins": [
|
46 | "react-require"
|
47 | ]
|
48 | }
|
49 | ```
|