Skip to content

Commit 7de3a0b

Browse files
arati-1harish-sethuramanholly1238gaearon
authored
[Beta] Added documentation for React.createFactory (#5120)
* Added documentation for React.createFactory * Update beta/src/content/apis/react/createFactory.md Co-authored-by: Strek <ssharishkumar@gmail.com> * minor editorial updates (#2) * Update createFactory.md * Update createFactory.md * edits Co-authored-by: Strek <ssharishkumar@gmail.com> Co-authored-by: Holly Sweeney <77758406+holly1238@users.noreply.github.com> Co-authored-by: Dan Abramov <dan.abramov@gmail.com>
1 parent 3a9b0c8 commit 7de3a0b

File tree

4 files changed

+144
-8
lines changed

4 files changed

+144
-8
lines changed

beta/src/components/MDX/ExpandableCallout.tsx

+10-1
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,25 @@ import {useRef} from 'react';
66
import * as React from 'react';
77
import cn from 'classnames';
88
import {IconNote} from '../Icon/IconNote';
9+
import {IconWarning} from '../Icon/IconWarning';
910
import {IconGotcha} from '../Icon/IconGotcha';
1011

11-
type CalloutVariants = 'gotcha' | 'note' | 'wip';
12+
type CalloutVariants = 'deprecated' | 'gotcha' | 'note' | 'wip';
1213

1314
interface ExpandableCalloutProps {
1415
children: React.ReactNode;
1516
type: CalloutVariants;
1617
}
1718

1819
const variantMap = {
20+
deprecated: {
21+
title: 'Deprecated',
22+
Icon: IconWarning,
23+
containerClasses: 'bg-red-5 dark:bg-red-60 dark:bg-opacity-20',
24+
textColor: 'text-red-50 dark:text-red-40',
25+
overlayGradient:
26+
'linear-gradient(rgba(249, 247, 243, 0), rgba(249, 247, 243, 1)',
27+
},
1928
note: {
2029
title: 'Note',
2130
Icon: IconNote,

beta/src/components/MDX/MDXComponents.tsx

+4
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,9 @@ const Wip = ({children}: {children: React.ReactNode}) => (
7979
const Gotcha = ({children}: {children: React.ReactNode}) => (
8080
<ExpandableCallout type="gotcha">{children}</ExpandableCallout>
8181
);
82+
const Deprecated = ({children}: {children: React.ReactNode}) => (
83+
<ExpandableCallout type="deprecated">{children}</ExpandableCallout>
84+
);
8285
const Note = ({children}: {children: React.ReactNode}) => (
8386
<ExpandableCallout type="note">{children}</ExpandableCallout>
8487
);
@@ -369,6 +372,7 @@ export const MDXComponents = {
369372
return <div className="max-w-4xl ml-0 2xl:mx-auto">{children}</div>;
370373
},
371374
Gotcha,
375+
Deprecated,
372376
Wip,
373377
HomepageHero,
374378
Illustration,

beta/src/content/apis/react/createFactory.md

+129-5
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,143 @@
22
title: createFactory
33
---
44

5-
<Wip>
5+
<Deprecated>
66

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.
108

9+
</Deprecated>
1110

1211
<Intro>
1312

13+
`createFactory` lets you create a function that produces React elements of a given type.
14+
1415
```js
15-
React.createFactory(type)
16+
const factory = createFactory(type)
1617
```
1718

1819
</Intro>
1920

2021
<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`.

beta/src/sidebarReference.json

+1-2
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,7 @@
3737
},
3838
{
3939
"title": "createFactory",
40-
"path": "/apis/react/createFactory",
41-
"wip": true
40+
"path": "/apis/react/createFactory"
4241
},
4342
{
4443
"title": "createRef",

0 commit comments

Comments
 (0)