@@ -19,20 +19,16 @@ As a brief refresher, controlled inputs involve two key pieces:
19192 . An _ onChange_ prop function to receive updates to the input
2020
2121This 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
3834The 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
4339In 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.
46432 . There is no such ` onChange ` event available for a ContentEditable element.
4744
4845State 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
5350The ` EditorState ` object is a complete snapshot of the state of the editor,
5451including 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
5956import {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