2

Here is my code:

import React, { useState } from "react";
import { CKEditor } from "@ckeditor/ckeditor5-react";
import ClassicEditor from "@ckeditor/ckeditor5-build-classic";

const Followups = () => {
  const [clicked, setClicked] = useState(false);
  const [text, setText] = useState("");
  console.log("START", clicked);

  const buttonClicked = () => {
    setClicked(!clicked);
    // setClicked(false);
  };

  //   const changeState = () => {
  //     setClicked(false);
  //     console.log("State set to false ");
  //   };

  return (
    <>
      {clicked && (
        <CKEditor
          editor={ClassicEditor}
          data={text}
          onChange={(event, editor) => {
            const data = editor.getData();
            setText(data);
        
          }}
          config={{
            toolbar: [
              "heading",
              "|",
              "bold",
              "italic",
              "blockQuote",
              "numberedList",
              "bulletedList",
              "|",
              "undo",
              "redo",
            ],
          }}
        />
      )}
      
      <button onClick={buttonClicked}>Follow-ups</button>
    </>
  );
};
export default Followups;

This is working properly without any error but I am not getting the desired output. Whenever I click the button it shows me another ckEditor but when I press the button for the second time it hides the ckEditor (toggle). but I want that whenever I press the button it shows me another ckEditor. I need help kindly anybody help me out

3
  • If you want another CKEditor component to be added whenever you click on the button, you should use an array to maintain the list of CKEditor components in use. On button click, push the new instance of the component to the array. Return array.map((CKEditorComponent)=>return CKEditorComponent) Commented Sep 8, 2021 at 5:37
  • Thanks a lot Hariom Balhara for your quick response. I try your logic and its working now! Commented Sep 8, 2021 at 10:30
  • Cool. I have posted it as the answer. Can you please accept the answer. Commented Sep 8, 2021 at 12:47

2 Answers 2

1

Followup.jsx File

    import React, { useState } from "react";
import { CKEditor } from "@ckeditor/ckeditor5-react";
import ClassicEditor from "@ckeditor/ckeditor5-build-classic";

const Followups = (props) => {
  const [text, setText] = useState("");
  console.log(text);
  return (
    <>
      <CKEditor
        editor={ClassicEditor}
        data={text}
        onChange={(event, editor) => {
          const data = editor.getData();
          setText(data);
          
        }}
        config={{
          toolbar: [
            "heading",
            "|",
            "bold",
            "italic",
            "blockQuote",
            "numberedList",
            "bulletedList",
            "|",
            "undo",
            "redo",
          ],
        }}
      />

      
    </>
  );
};
export default Followups;

App.jsx

    import React, { useState } from "react";

const [clicked, setClicked] = useState([]);
const buttonClicked = (event) => {
    event.preventDefault();
    let newValue = true;
    setClicked((preValue) => [...preValue, newValue]);
   
  };

  return (
    <>
      {clicked.map((data, index) => {
        return <Followups  />;
      })}
      <button className="btn btn-outline-success" onClick={buttonClicked}>
        Follow-ups
      </button>
    </>
  );
Sign up to request clarification or add additional context in comments.

Comments

0

If you want another CKEditor component to be added whenever you click on the button, you should use an array to maintain the list of CKEditor components in use. On button click, push the new instance of the component to the array. Return array.map((CKEditorComponent)=>return CKEditorComponent)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.