Skip to content

Commit dc42799

Browse files
committed
edits
1 parent 1747c75 commit dc42799

File tree

3 files changed

+116
-35
lines changed

3 files changed

+116
-35
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

+102-34
Original file line numberDiff line numberDiff line change
@@ -2,75 +2,143 @@
22
title: createFactory
33
---
44

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>
610

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>
812

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

1115
```js
12-
React.createFactory(type)
16+
const factory = createFactory(type)
1317
```
14-
</Intro>
15-
16-
<Note>
1718

18-
`createFactory` is considered legacy, and we encourage you to either use **JSX** or use `React.createElement()` directly instead.
19-
</Note>
19+
</Intro>
2020

2121
<InlineToc />
2222

23+
---
24+
2325
## Usage {/*usage*/}
2426

2527
### Creating React elements {/*creating-react-elements*/}
2628

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)
2830

2931
<Sandpack>
3032

31-
``` js App.js
32-
import React from 'react';
33+
```js App.js
34+
import { createFactory } from 'react';
3335

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!')
3942
}
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');
4864
};
4965
```
50-
``` js index.js
5166

52-
import {createRoot} from 'react-dom/client';
53-
import App from './App.js';
67+
</Sandpack>
5468

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>
5772

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+
};
5883
```
84+
5985
</Sandpack>
6086

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+
61109
### `createFactory(type)` {/*createfactory*/}
62110

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

65120
```js
66-
const myElement= React.createFactory(type)
121+
import { createFactory } from 'react';
67122

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+
}
68136
```
69137

70138
#### Parameters {/*parameters*/}
71139

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)).
73141

74142
#### Returns {/*returns*/}
75143

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

Comments
 (0)