const Editor = ({ socketRef, roomId, onCodeChange }) => {
const editorRef = useRef(null);
useEffect(() => {
async function init() {
editorRef.current = Codemirror.fromTextArea(
document.getElementById('realtimeEditor'),
{
mode: { name: 'javascript', json: true },
theme: 'dracula',
autoCloseTags: true,
autoCloseBrackets: true,
lineNumbers: true,
}
);
editorRef.current.on('change', (instance, changes) => {
const { origin } = changes;
const code = instance.getValue();
onCodeChange(code);
if (origin !== 'setValue') {
socketRef.current.emit(ACTIONS.CODE_CHANGE, {
roomId,
code,
});
}
});
}
init();
}, []);
useEffect(() => {
if (socketRef.current) {
socketRef.current.on(ACTIONS.CODE_CHANGE, ({ code }) => {
if (code !== null) {
editorRef.current.setValue(code);
}
});
}
return () => {
socketRef.current.off(ACTIONS.CODE_CHANGE);
};
}, [socketRef.current]);
return <textarea id="realtimeEditor"></textarea>;
};
export default Editor;
I wanted to resolve this error but I have tried many things and I am not able to resolve this issue.
I have searched everywhere and even chatgpt cannot solve this issue. If anyone knows how to solve it then please let me know.
I’ve added all the libraries correctly and I don’t know why this error is popping up.
New contributor
Superstring is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.