react-draft-wysiwyg icon indicating copy to clipboard operation
react-draft-wysiwyg copied to clipboard

Unable to update toolbar options after mount

Open bolatuly opened this issue 5 years ago • 5 comments

I'm trying to dynamically change toolbar options based on user preferences. But seems like we can't change toolbar props after a component has been mounted. I made a simple example below of what I'm trying to achieve.

import React, {useState} from "react";
import "./styles.css";

import { Editor } from 'react-draft-wysiwyg';
import 'react-draft-wysiwyg/dist/react-draft-wysiwyg.css';

export default function App() {

  const one = ['inline', 'blockType'];
  const two = ['inline'];

  const [options, setOptions] = useState(two);

  const onClick = () => {
    setOptions(one); 
  }

  return (
    <div className="App">
    <button onClick={onClick}>Switch</button>
    <Editor 
      toolbar={{options}}
      />
    </div>
  );
}

Any workaround to this?

Thank you in advance for your help. Hope this repo still alive.

bolatuly avatar Jun 12 '20 05:06 bolatuly

Hey @bolatuly, could you figure out a workaround to this?

Nachoferra1894 avatar Mar 04 '22 13:03 Nachoferra1894

Have you found the solution? still stuck on it

rakibsxyz avatar Sep 14 '22 10:09 rakibsxyz

Add some dummy state and update it when you change options to force the component to update https://stackoverflow.com/questions/65284303/how-to-force-an-update-for-a-functional-component

jestrickler avatar Sep 14 '22 13:09 jestrickler

thanks for the reply. I tried this one but still not working. const [toolbarToggle, setTollbarToggle] = useState(false)

changed the setTollbarToggle state when options change but still its not working. May be its mounted and the options don't rerender after the first render.

rakibsxyz avatar Sep 15 '22 03:09 rakibsxyz

try this, it worked for me:

import React, { useState } from "react";
import "./styles.css";

import { Editor } from "react-draft-wysiwyg";
import "react-draft-wysiwyg/dist/react-draft-wysiwyg.css";

export default function App() {
  const one = ["inline", "blockType"];
  const two = ["inline"];

  const [options, setOptions] = useState(two);

  const onClick = () => {
    console.log('options', options)
    setOptions(JSON.stringify(options) === JSON.stringify(one) ? two : one);
  };

  return ( 
    <div className="App">
      <button onClick={onClick}>Switch</button>
      <Editor key={options} toolbar={{ options }} />
    </div>
  );
}

jestrickler avatar Sep 15 '22 13:09 jestrickler