Skip to content

Commit

Permalink
Adding Hooks in docs (facebookarchive#2004)
Browse files Browse the repository at this point in the history
Summary:
Adds React Hooks as examples in docs facebookarchive#1987
Pull Request resolved: facebookarchive#2004

Reviewed By: claudiopro

Differential Revision: D14783281

Pulled By: claudiopro

fbshipit-source-id: 7edcdb3ee4e32afd1197ea69fed968538243eccd
  • Loading branch information
charliewilco authored and mmissey committed Mar 24, 2020
1 parent 8ab2396 commit dc9db73
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 0 deletions.
32 changes: 32 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,38 @@ ReactDOM.render(
);
```

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.


```js
function MyEditor() {
const [editorState, setEditorState] = React.useState(
EditorState.createEmpty()
);

const editor = React.useRef(null);

function focusEditor() {
editor.current.focus();
}

React.useEffect(() => {
focusEditor()
}, []);

return (
<div onClick={focusEditor}>
<Editor
ref={editor}
editorState={editorState}
onChange={editorState => setEditorState(editorState)}
/>
</div>
);
}

```

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.

Because Draft.js supports unicode, you must have the following meta tag in the `<head>` `</head>` block of your HTML file:
Expand Down
26 changes: 26 additions & 0 deletions docs/Overview.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,32 @@ ReactDOM.render(
);
```

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.

```js
import React from 'react';
import ReactDOM from 'react-dom';
import {Editor, EditorState} from 'draft-js';

function MyEditor() {
const [editorState, setEditorState] = React.useState(
EditorState.createEmpty()
);

return (
<Editor
editorState={editorState}
onChange={editorState => setEditorState(editorState)}
/>
);
}

ReactDOM.render(
<MyEditor />,
document.getElementById('container')
);
```

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

```html
Expand Down

0 comments on commit dc9db73

Please sign in to comment.