Skip to content

Commit 271b6ce

Browse files
committed
refactor: move refs to context so we can access them inside the info table
1 parent 55464e8 commit 271b6ce

File tree

4 files changed

+47
-14
lines changed

4 files changed

+47
-14
lines changed

src/components/App.js

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,19 +7,21 @@ import parser from '../parser';
77

88
import { initialValues } from '../constants';
99
import ensureArray from '../lib/ensureArray';
10+
import { AppContextProvider, useAppContext } from './Context';
1011

1112
let cycle = 0;
1213

1314
function App() {
1415
const [html, setHtml] = useState(initialValues.html);
1516
const [js, setJs] = useState(initialValues.js);
16-
const htmlRoot = useRef();
17+
18+
const { htmlEditorRef, htmlPreviewRef, jsEditorRef } = useAppContext();
1719

1820
const [result, setResult] = useState({});
1921
const [elements, setElements] = useState([]);
2022

2123
useEffect(() => {
22-
const parsed = parser.parse(htmlRoot.current, js);
24+
const parsed = parser.parse(htmlPreviewRef.current, js);
2325
setResult(parsed);
2426

2527
const elements = ensureArray(parsed.code).filter(
@@ -33,7 +35,7 @@ function App() {
3335
return () => {
3436
elements.forEach((el) => el.classList.remove('highlight'));
3537
};
36-
}, [html, js, htmlRoot.current]);
38+
}, [html, js, htmlPreviewRef.current]);
3739

3840
return (
3941
<div>
@@ -43,10 +45,15 @@ function App() {
4345

4446
<div className="space-y-8 px-8 pb-8">
4547
<div className="editor">
46-
<Editor mode="html" initialValue={html} onChange={setHtml} />
48+
<Editor
49+
mode="html"
50+
initialValue={html}
51+
onChange={setHtml}
52+
ref={htmlEditorRef}
53+
/>
4754
<div
4855
className="preview"
49-
ref={htmlRoot}
56+
ref={htmlPreviewRef}
5057
dangerouslySetInnerHTML={{ __html: html }}
5158
/>
5259
</div>
@@ -57,6 +64,7 @@ function App() {
5764
mode="javascript"
5865
initialValue={initialValues.js}
5966
onChange={setJs}
67+
ref={jsEditorRef}
6068
/>
6169
<div className="output">
6270
<span className="text-blue-600">&gt; </span>
@@ -67,11 +75,7 @@ function App() {
6775
<div>
6876
<div>
6977
{elements.map((x, idx) => (
70-
<ElementInfo
71-
key={`${cycle}-${idx}`}
72-
root={htmlRoot.current}
73-
element={x}
74-
/>
78+
<ElementInfo key={`${cycle}-${idx}`} element={x} />
7579
))}
7680
</div>
7781
</div>

src/components/Context.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import React, { useContext, useRef } from 'react';
2+
3+
export const AppContext = React.createContext();
4+
5+
function AppContextProvider(props) {
6+
const jsEditorRef = useRef();
7+
const htmlEditorRef = useRef();
8+
const htmlPreviewRef = useRef();
9+
10+
return (
11+
<AppContext.Provider
12+
value={{ jsEditorRef, htmlEditorRef, htmlPreviewRef }}
13+
{...props}
14+
/>
15+
);
16+
}
17+
18+
function useAppContext() {
19+
return useContext(AppContext);
20+
}
21+
22+
export { AppContextProvider, useAppContext };

src/components/Editor.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,10 @@ const options = {
3030
},
3131
};
3232

33-
function Editor({ onChange, mode, initialValue }) {
33+
function Editor({ onChange, mode, initialValue }, forwardRef) {
3434
const elem = useRef();
35-
const editor = useRef();
35+
const localRef = useRef();
36+
const editor = forwardRef || localRef;
3637

3738
useEffect(() => {
3839
editor.current = CodeMirror.fromTextArea(
@@ -56,4 +57,4 @@ function Editor({ onChange, mode, initialValue }) {
5657
return <textarea ref={elem} />;
5758
}
5859

59-
export default Editor;
60+
export default React.forwardRef(Editor);

src/index.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
import React from 'react';
22
import ReactDOM from 'react-dom';
33
import App from './components/App';
4+
import { AppContextProvider } from './components/Context';
45

5-
ReactDOM.render(<App />, document.getElementById('app'));
6+
ReactDOM.render(
7+
<AppContextProvider>
8+
<App />
9+
</AppContextProvider>,
10+
document.getElementById('app'),
11+
);

0 commit comments

Comments
 (0)