Skip to content

Commit 5420ce7

Browse files
MichaelDeBoeyfacebook-github-bot
authored andcommitted
Update QuickStart-API-Basics.md (#2491)
Summary: Pull Request resolved: facebookarchive/draft-js#2491 Reviewed By: elboman Differential Revision: D22788091 Pulled By: mrkev fbshipit-source-id: c83b95a355f0aecfaf6f5ed35a4fdde3b4fbe05a
1 parent 38b44a4 commit 5420ce7

File tree

1 file changed

+23
-30
lines changed

1 file changed

+23
-30
lines changed

docs/QuickStart-API-Basics.md

Lines changed: 23 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -19,20 +19,16 @@ As a brief refresher, controlled inputs involve two key pieces:
1919
2. An _onChange_ prop function to receive updates to the input
2020

2121
This approach allows the component that composes the input to have strict
22-
control over the state of the input, while still allowing updates to the DOM
23-
to provide information about the text that the user has written.
22+
control over the state of the input, while still allowing updates to the DOM to
23+
provide information about the text that the user has written.
2424

2525
```js
26-
class MyInput extends React.Component {
27-
constructor(props) {
28-
super(props);
29-
this.state = {value: ''};
30-
this.onChange = evt => this.setState({value: evt.target.value});
31-
}
32-
render() {
33-
return <input value={this.state.value} onChange={this.onChange} />;
34-
}
35-
}
26+
const MyInput = () => {
27+
const [value, setValue] = useState('');
28+
const onChange = (evt) => setValue(evt.target.value);
29+
30+
return <input value={value} onChange={onChange} />;
31+
};
3632
```
3733

3834
The top-level component can maintain control over the input state via this
@@ -42,13 +38,14 @@ The top-level component can maintain control over the input state via this
4238

4339
In a React rich text scenario, however, there are two clear problems:
4440

45-
1. A string of plaintext is insufficient to represent the complex state of a rich editor.
41+
1. A string of plaintext is insufficient to represent the complex state of a
42+
rich editor.
4643
2. There is no such `onChange` event available for a ContentEditable element.
4744

4845
State is therefore represented as a single immutable
49-
[EditorState](/docs/api-reference-editor-state) object, and
50-
`onChange` is implemented within the `Editor` core to provide this state
51-
value to the top level.
46+
[EditorState](/docs/api-reference-editor-state) object, and `onChange` is
47+
implemented within the `Editor` core to provide this state value to the top
48+
level.
5249

5350
The `EditorState` object is a complete snapshot of the state of the editor,
5451
including contents, cursor, and undo/redo history. All changes to content and
@@ -58,19 +55,15 @@ this remains efficient due to data persistence across immutable objects.
5855
```js
5956
import {Editor, EditorState} from 'draft-js';
6057

61-
class MyEditor extends React.Component {
62-
constructor(props) {
63-
super(props);
64-
this.state = {editorState: EditorState.createEmpty()};
65-
this.onChange = editorState => this.setState({editorState});
66-
}
67-
68-
render() {
69-
return (
70-
<Editor editorState={this.state.editorState} onChange={this.onChange} />
71-
);
72-
}
73-
}
58+
const MyInput = () => {
59+
const [editorState, setEditorState] = useState(() =>
60+
EditorState.createEmpty(),
61+
);
62+
63+
return <Editor editorState={editorState} onChange={setEditorState} />;
64+
};
7465
```
7566

76-
For any edits or selection changes that occur in the editor DOM, your `onChange` handler will execute with the latest `EditorState` object based on those changes.
67+
For any edits or selection changes that occur in the editor DOM, your `onChange`
68+
handler will execute with the latest `EditorState` object based on those
69+
changes.

0 commit comments

Comments
 (0)