Skip to content

Commit 250cf04

Browse files
committed
website: update example.
1 parent 7d69894 commit 250cf04

File tree

1 file changed

+10
-11
lines changed

1 file changed

+10
-11
lines changed

www/src/pages/examples/ExampleRef.tsx

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,22 @@
11
import CodeMirror, { ReactCodeMirrorRef } from '@uiw/react-codemirror';
22
import { langs } from '@uiw/codemirror-extensions-langs';
3-
import { useEffect, useRef } from 'react';
3+
import { useRef } from 'react';
44

55
export function Component() {
6-
const $edit = useRef<ReactCodeMirrorRef>(null);
7-
useEffect(() => {
8-
console.log('$edit:', $edit);
9-
setTimeout(() => {
10-
console.log('$edit:view:', $edit.current?.view);
11-
console.log('$edit:state:', $edit.current?.state);
12-
}, 300);
13-
console.log('$edit:', $edit.current?.view);
14-
}, [$edit]);
6+
let $edit = useRef<ReactCodeMirrorRef | null>(null);
7+
function refCallack(editor: ReactCodeMirrorRef) {
8+
if (!$edit.current && editor?.editor && editor?.state && editor?.view) {
9+
// first time we got ref, similar to useEffect
10+
console.log(editor); // do something with editor
11+
$edit.current = editor; // store it
12+
}
13+
}
1514
return (
1615
<div>
1716
<CodeMirror
1817
value={`console.log('hello')`}
1918
theme="none"
20-
ref={$edit}
19+
ref={refCallack}
2120
height="400px"
2221
width="100%"
2322
style={{ margin: '0 0 23px 0', flex: 1 }}

0 commit comments

Comments
 (0)