Skip to content
This repository was archived by the owner on Feb 6, 2023. It is now read-only.

Commit f9f5fd6

Browse files
charliewilcofacebook-github-bot
authored andcommitted
Adding Hooks in docs (#2004)
Summary: Adds React Hooks as examples in docs #1987 Pull Request resolved: #2004 Reviewed By: claudiopro Differential Revision: D14783281 Pulled By: claudiopro fbshipit-source-id: 7edcdb3ee4e32afd1197ea69fed968538243eccd
1 parent dc58df8 commit f9f5fd6

File tree

2 files changed

+58
-0
lines changed

2 files changed

+58
-0
lines changed

README.md

+32
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,38 @@ ReactDOM.render(
9191
);
9292
```
9393

94+
Since the release of React 16.8, use can use [Hooks](https://reactjs.org/docs/hooks-intro.html) as a way to work with `EditorState` without using a class.
95+
96+
97+
```js
98+
function MyEditor() {
99+
const [editorState, setEditorState] = React.useState(
100+
EditorState.createEmpty()
101+
);
102+
103+
const editor = React.useRef(null);
104+
105+
function focusEditor() {
106+
editor.current.focus();
107+
}
108+
109+
React.useEffect(() => {
110+
focusEditor()
111+
}, []);
112+
113+
return (
114+
<div onClick={focusEditor}>
115+
<Editor
116+
ref={editor}
117+
editorState={editorState}
118+
onChange={editorState => setEditorState(editorState)}
119+
/>
120+
</div>
121+
);
122+
}
123+
124+
```
125+
94126
Note that the editor itself is only as tall as its contents. In order to give users a visual cue, we recommend setting a border and a minimum height via the `.DraftEditor-root` CSS selector, or using a wrapper div like in the above example.
95127

96128
Because Draft.js supports unicode, you must have the following meta tag in the `<head>` `</head>` block of your HTML file:

docs/Overview.md

+26
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,32 @@ ReactDOM.render(
6666
);
6767
```
6868

69+
Since the release of React 16.8, use can use [Hooks](https://reactjs.org/docs/hooks-intro.html) as a way to work with `EditorState` without using a class.
70+
71+
```js
72+
import React from 'react';
73+
import ReactDOM from 'react-dom';
74+
import {Editor, EditorState} from 'draft-js';
75+
76+
function MyEditor() {
77+
const [editorState, setEditorState] = React.useState(
78+
EditorState.createEmpty()
79+
);
80+
81+
return (
82+
<Editor
83+
editorState={editorState}
84+
onChange={editorState => setEditorState(editorState)}
85+
/>
86+
);
87+
}
88+
89+
ReactDOM.render(
90+
<MyEditor />,
91+
document.getElementById('container')
92+
);
93+
```
94+
6995
Because Draft.js supports unicode, you must have the following meta tag in the `<head></head>` block of your HTML file:
7096

7197
```html

0 commit comments

Comments
 (0)