How to use RichUtils with React Hooks

Here is an example on how to use RichUtils in Draft.js. The code translated to React Hooks:

import React, { useEffect, useState }   from 'react';
import {Editor, EditorState, RichUtils} from 'draft-js';

const MyEditor = () => {

    const [editorState, setEditorState] = useState(EditorState.createEmpty());

    const boldText = (e) => {
        // onMouseDown and e.preventDefault because editor losses focus if you use onClick
        let nextState = RichUtils.toggleInlineStyle(editorState, 'BOLD');
    return (
        <div className = 'MyEditor'>
            <button onMouseDown = {boldText}>Bold</button>
                editorState = {editorState}
                onChange = {editorState => setEditorState(editorState)}

export default MyEditor;

Hi, I'm Erik, an engineer from Barcelona. If you like the post or have any comments, say hi.