import React, { useState, useEffect, useRef } from "react";
import styles from "./boarder.module.css";
import ReactHtmlParser from "html-react-parser";
export default function MyEditor() {
const editorRef = useRef();
const [editorLoaded, setEditorLoaded] = useState(false);
const { CKEditor, ClassicEditor } = editorRef.current || {};
const [boardContent, setBoardContent] = useState({
title: "",
content: "",
});
const [viewContent, setViewContent] = useState([]);
useEffect(() => {
editorRef.current = {
CKEditor: require("@ckeditor/ckeditor5-react").CKEditor,
ClassicEditor: require("@ckeditor/ckeditor5-build-classic"),
};
setEditorLoaded(true);
}, []);
const getValue = (e) => {
const { name, value } = e.target;
setBoardContent({
...boardContent,
[name]: value,
});
console.log(boardContent);
};
return editorLoaded ? (
<div>
<div className="center">
<h1 className={styles.ct}>Contents Review</h1>
<div className={styles.movie_container}>
{viewContent.map((e, i) => (
<div key={i}>
<h2>{e.title}</h2>
<div>{ReactHtmlParser(e.content)}</div>
</div>
))}
;
</div>
</div>
<CKEditor
editor={ClassicEditor}
data={CKEditor.data}
onChange={(event, editor) => {
const data = editor.getData();
console.log({ event, editor, data });
setBoardContent({ ...boardContent, content: data });
console.log(boardContent);
}}
/>
<button
className={styles.submit_button}
onClick={() => {
setViewContent(viewContent.concat({ ...boardContent }));
}}
>
입력
</button>
</div>
) : (
<div>Editor loading</div>
);
}
최적화가 필요한 이유
- 실제 서비스에 도입되면 수정해야 할 상황이 많을텐데
그때마다 코드를 보면서 다시 공부할 시간이 없음.
- 가독성, 실제 코드 기능에 방해되면 제거해야
- 리소스 낭비를 줄이기 위해
- 팀원과의 원활한 협업을 위해
아직 내 수준이 못미쳐서 최적화할 단계는 아닌듯함