|
2 | 2 | title: createFactory
|
3 | 3 | ---
|
4 | 4 |
|
5 |
| -<Intro> |
| 5 | +<Deprecated> |
| 6 | + |
| 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. |
| 8 | + |
| 9 | +</Deprecated> |
6 | 10 |
|
7 |
| -`createFactory` lets you create a function that creates a React element of a given `type`. The `type` argument can be either a tag name string (such as `div` or `span`), a React component type (a class or a function), or a React fragment type. |
| 11 | +<Intro> |
8 | 12 |
|
9 |
| -`createFactory` is typically invoked if you are using [React without JSX.](https://beta.reactjs.org/learn/add-react-to-a-website#run-the-jsx-preprocessor) |
| 13 | +`createFactory` lets you create a function that produces React elements of a given type. |
10 | 14 |
|
11 | 15 | ```js
|
12 |
| -React.createFactory(type) |
| 16 | +const factory = createFactory(type) |
13 | 17 | ```
|
14 |
| -</Intro> |
15 |
| - |
16 |
| -<Note> |
17 | 18 |
|
18 |
| -`createFactory` is considered legacy, and we encourage you to either use **JSX** or use `React.createElement()` directly instead. |
19 |
| -</Note> |
| 19 | +</Intro> |
20 | 20 |
|
21 | 21 | <InlineToc />
|
22 | 22 |
|
| 23 | +--- |
| 24 | + |
23 | 25 | ## Usage {/*usage*/}
|
24 | 26 |
|
25 | 27 | ### Creating React elements {/*creating-react-elements*/}
|
26 | 28 |
|
27 |
| -In this example, we can render a React element of the `type` `button`. |
| 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) |
28 | 30 |
|
29 | 31 | <Sandpack>
|
30 | 32 |
|
31 |
| -``` js App.js |
32 |
| -import React from 'react'; |
| 33 | +```js App.js |
| 34 | +import { createFactory } from 'react'; |
33 | 35 |
|
34 |
| -const MyButton = () => |
35 |
| - (React.createFactory("button"))({ |
36 |
| - onClick: (evt) => { |
37 |
| - evt.preventDefault(); |
38 |
| - alert("I was created by createFactory()"); |
| 36 | +const button = createFactory('button'); |
| 37 | + |
| 38 | +export default function App() { |
| 39 | + return button({ |
| 40 | + onClick: () => { |
| 41 | + alert('Clicked!') |
39 | 42 | }
|
40 |
| - }, 'Click'); |
41 |
| - |
42 |
| -export default function App(){ |
43 |
| - return ( |
44 |
| - <div> |
45 |
| - {MyButton()} |
46 |
| - </div> |
47 |
| - ) |
| 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'); |
48 | 64 | };
|
49 | 65 | ```
|
50 |
| -``` js index.js |
51 | 66 |
|
52 |
| -import {createRoot} from 'react-dom/client'; |
53 |
| -import App from './App.js'; |
| 67 | +</Sandpack> |
54 | 68 |
|
55 |
| -const root = createRoot(document.getElementById('root')); |
56 |
| -root.render(<App />); |
| 69 | +Alternatively, you can convert it to use [JSX:](/learn/writing-markup-with-jsx) |
| 70 | + |
| 71 | +<Sandpack> |
57 | 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 | +}; |
58 | 83 | ```
|
| 84 | + |
59 | 85 | </Sandpack>
|
60 | 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 | + |
61 | 109 | ### `createFactory(type)` {/*createfactory*/}
|
62 | 110 |
|
63 |
| -Call `createFactory(type)` to create a function that creates a React element of a given `type`. |
| 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`. |
64 | 119 |
|
65 | 120 | ```js
|
66 |
| -const myElement= React.createFactory(type) |
| 121 | +import { createFactory } from 'react'; |
67 | 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 | +} |
68 | 136 | ```
|
69 | 137 |
|
70 | 138 | #### Parameters {/*parameters*/}
|
71 | 139 |
|
72 |
| -* `type`: The `type` argument can be either a tag name string (such as `div` or `span`), a React component type (a class or a function), or a React fragment type. |
| 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)). |
73 | 141 |
|
74 | 142 | #### Returns {/*returns*/}
|
75 | 143 |
|
76 |
| - Returns a function that can be used to create a React element of the given `type`. |
| 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