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

Adding Hooks in docs #2004

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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