0

im new to react and just have to update a value in onChange to filter some tags. But When I do that, it causes an infinite loop. How can I prevent that?

const Editor = ({ onChange, name, value }) => {

  const modules = {
    toolbar: [
      [{ 'header': '1'}, {'header': '2'}, { 'font': [] }],
      [{size: []}],
      ['bold', 'italic', 'underline','strike', 'blockquote'],
      [{'list': 'ordered'}, {'list': 'bullet'}, {'indent': '-1'}, {'indent': '+1'}],
      ['link'],
      ['clean']
    ],
    clipboard: {
      matchVisual: false
    }
  }

  return (
    <ReactQuill
      theme="snow"
      value={value}
      modules={modules}
      onChange={(content, event, editor) => {
        const cleanedContent = content?.replace(/<p><br><\/p>/g, '<br>');
        console.log('test');
        onChange({ target: { name, value: cleanedContent  } });
      }}/>
  );
};

export default Editor;
xDrago
  • 1,772
  • 2
  • 20
  • 37

1 Answers1

0

You can replace and set tags manually, so the component will not render infinitely and the output is a clean string. In this way, the ReactQuill component thinks that there are already <p> and <br> tags and won't set them automatically.

<ReactQuill
  value={!value ? '<br>' : `<p>${value}</p>`}
  onChange={(val: string) => {
    handleUpdate(val.replaceAll(/<\/?p[^>]*>/g, '').replace('<br>', ''));
  }}
/>
Plattonpp
  • 1
  • 1