|
2 | 2 | title: createFactory
|
3 | 3 | ---
|
4 | 4 |
|
5 |
| -<Wip> |
| 5 | +<Deprecated> |
6 | 6 |
|
7 |
| -This section is incomplete, please see the old docs for [createFactory.](https://reactjs.org/docs/react-api.html#createfactory) |
8 |
| - |
9 |
| -</Wip> |
| 7 | +This API will be removed in a future major version of React. Use [JSX](/learn/writing-markup-with-jsx) or [`createElement`](/api/react/createElement) instead. |
10 | 8 |
|
| 9 | +</Deprecated> |
11 | 10 |
|
12 | 11 | <Intro>
|
13 | 12 |
|
| 13 | +`createFactory` lets you create a function that produces React elements of a given type. |
| 14 | + |
14 | 15 | ```js
|
15 |
| -React.createFactory(type) |
| 16 | +const factory = createFactory(type) |
16 | 17 | ```
|
17 | 18 |
|
18 | 19 | </Intro>
|
19 | 20 |
|
20 | 21 | <InlineToc />
|
| 22 | + |
| 23 | +--- |
| 24 | + |
| 25 | +## Usage {/*usage*/} |
| 26 | + |
| 27 | +### Creating React elements {/*creating-react-elements*/} |
| 28 | + |
| 29 | +You shouldn't use `createFactory` in new code. In the existing code, it's typically used as an alternative to [JSX:](/learn/writing-markup-with-jsx) |
| 30 | + |
| 31 | +<Sandpack> |
| 32 | + |
| 33 | +```js App.js |
| 34 | +import { createFactory } from 'react'; |
| 35 | + |
| 36 | +const button = createFactory('button'); |
| 37 | + |
| 38 | +export default function App() { |
| 39 | + return button({ |
| 40 | + onClick: () => { |
| 41 | + alert('Clicked!') |
| 42 | + } |
| 43 | + }, 'Click me'); |
| 44 | +} |
| 45 | +``` |
| 46 | + |
| 47 | +</Sandpack> |
| 48 | + |
| 49 | +Since `createFactory` has been deprecated, you need to remove it from your project's code. |
| 50 | + |
| 51 | +For example, you can convert it to use [`createElement`](/api/react/createElement) instead of `createFactory` like this: |
| 52 | + |
| 53 | +<Sandpack> |
| 54 | + |
| 55 | +```js App.js |
| 56 | +import { createElement } from 'react'; |
| 57 | + |
| 58 | +export default function App() { |
| 59 | + return createElement('button', { |
| 60 | + onClick: () => { |
| 61 | + alert('Clicked!') |
| 62 | + } |
| 63 | + }, 'Click me'); |
| 64 | +}; |
| 65 | +``` |
| 66 | + |
| 67 | +</Sandpack> |
| 68 | + |
| 69 | +Alternatively, you can convert it to use [JSX:](/learn/writing-markup-with-jsx) |
| 70 | + |
| 71 | +<Sandpack> |
| 72 | + |
| 73 | +```js App.js |
| 74 | +export default function App() { |
| 75 | + return ( |
| 76 | + <button onClick={() => { |
| 77 | + alert('Clicked!'); |
| 78 | + }}> |
| 79 | + Click me |
| 80 | + </button> |
| 81 | + ); |
| 82 | +}; |
| 83 | +``` |
| 84 | + |
| 85 | +</Sandpack> |
| 86 | + |
| 87 | +Every pattern that uses `createFactory` can be converted to either of the two styles above. |
| 88 | + |
| 89 | +<DeepDive title="How is createFactory implemented?"> |
| 90 | + |
| 91 | +The full implementation of `createFactory` looks like this: |
| 92 | + |
| 93 | +```js |
| 94 | +import { createElement } from 'react'; |
| 95 | + |
| 96 | +function createFactory(type) { |
| 97 | + return createElement.bind(null, type); |
| 98 | +} |
| 99 | +``` |
| 100 | + |
| 101 | +If your project uses `createFactory` a lot, you may copy this helper into your project or publish it on npm. |
| 102 | + |
| 103 | +</DeepDive> |
| 104 | + |
| 105 | +--- |
| 106 | + |
| 107 | +## Reference {/*reference*/} |
| 108 | + |
| 109 | +### `createFactory(type)` {/*createfactory*/} |
| 110 | + |
| 111 | + |
| 112 | +<Deprecated> |
| 113 | + |
| 114 | +This API will be removed in a future major version of React. Use [JSX](/learn/writing-markup-with-jsx) or [`createElement`](/api/react/createElement) instead. |
| 115 | + |
| 116 | +</Deprecated> |
| 117 | + |
| 118 | +Call `createFactory(type)` to create a factory function which produces React elements of a given `type`. |
| 119 | + |
| 120 | +```js |
| 121 | +import { createFactory } from 'react'; |
| 122 | + |
| 123 | +const button = createFactory('button'); |
| 124 | +``` |
| 125 | + |
| 126 | +Then you can use it to create React elements without JSX: |
| 127 | + |
| 128 | +```js |
| 129 | +export default function App() { |
| 130 | + return button({ |
| 131 | + onClick: () => { |
| 132 | + alert('Clicked!') |
| 133 | + } |
| 134 | + }, 'Click me'); |
| 135 | +} |
| 136 | +``` |
| 137 | + |
| 138 | +#### Parameters {/*parameters*/} |
| 139 | + |
| 140 | +* `type`: The `type` argument must be a valid React component type. For example, it could be a tag name string (such as `'div'` or `'span'`), or a React component (a function, a class, or a special component like [`Fragment`](/apis/react/Fragment)). |
| 141 | + |
| 142 | +#### Returns {/*returns*/} |
| 143 | + |
| 144 | +Returns a factory function. That factory function receives a `props` object as the first argument, followed by a list of `...children` arguments, and returns a React element with the given `type`, `props` and `children`. |
0 commit comments