New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Issues with multiple instances on same page? [HELP WANTED] #504
Labels
Comments
import React, { createContext, useReducer } from "react";
import CodeMirror from "@uiw/react-codemirror";
import { javascript } from "@codemirror/lang-javascript";
import { html } from "@codemirror/lang-html";
import { css } from "@codemirror/lang-css";
import { Iframe } from "./Iframe";
export const Context = createContext(null);
const initialData = {
js: "console.log('hello world!');",
css: "body {}",
html: "<div>test</div>"
};
export default function App() {
const [stateData, dispatch] = useReducer(
(state, action) => ({ ...state, ...action }),
initialData
);
return (
<Context.Provider value={stateData}>
<div>
<CodeMirror
value={stateData.js}
height="200px"
theme="dark"
extensions={[javascript({ jsx: true })]}
onChange={(value) => {
dispatch({ ...stateData, js: value });
}}
/>
<CodeMirror
value={stateData.html}
height="200px"
extensions={[html()]}
onChange={(value, viewUpdate) => {
dispatch({ ...stateData, html: value });
}}
/>
<CodeMirror
value={stateData.css}
height="200px"
extensions={[css()]}
onChange={(value, viewUpdate) => {
dispatch({ ...stateData, css: value });
}}
/>
<Iframe />
</div>
</Context.Provider>
);
}
import { useContext, useMemo, useRef } from "react";
import { Context } from "./App";
export const Iframe = () => {
const frameRef = useRef(null);
const data = useContext(Context);
const jsString = data.js
? `<script type="text/javascript">${data.js}</script>`
: "";
const cssString = data.css ? `<style>${data.css}</style>` : "";
const result = `<!DOCTYPE html><html><head>${cssString}</head><body>${data.html}</body>${jsString}</html>`;
useMemo(() => {
const blob = new Blob([result], { type: "text/html" });
if (frameRef.current) {
frameRef.current.src = URL.createObjectURL(blob);
}
}, [result]);
return <iframe ref={frameRef} title="Test"></iframe>;
}; |
Wow, @jaywcjlove, you actually gave me the whole solution, big thanks!. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
HEADS UP: This is an "I NEED HELP WITH MY CODE" type of post
I'm creating a html code editor(similar to codepen.io, bootsnipp.com, etc) and I need to use three(3) instances of the
react-codemirror
in the same page.One would be for HTML; a second one for CSS and a third one for JavaScript. The way I use it is as follow:
Furthermore, for the simplicity of the issue, I only pasted the HTML editor instance. The CodeEditor above makes references to the actual CodeMirror component and this one looks like this:
I initially though the problem was caused due to the browser not being able to differentiate between the three Editors(I always go for the easiest solution) so I went and gave them a different ID to each one. To my surprise, the issue still remained.
I then decided to wrap my
handleChange()
in auseCallback()
just as it looks below:I saw several posts that this was caused due to having the Editors update the same state over and over but I'm still unable to solve it. The solutions that I always find keep saying to wrap my functions in
useCallback()
and/or wrap the CodeMirror component inmemo()
. Any ideas?The current behavior is this:
Click to see image on cloudinary
The image might not totally show my issue but whenever I type something in any of the editors, the state gets restarted? what I mean is something like "one click per character" I need to keep clicking on x editor to keep typing. For some reason, whenever I click on another editor, the state does definetely gets restarted and don't know why.
The text was updated successfully, but these errors were encountered: