DOCS

How to use Livelink.js with React?

Start or join a session inside a useEffect hook

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
import { useEffect, useRef } from 'react';
import { PUBLIC_TOKEN, SCENE_UUID } from 'config.js';
export const Canvas = () => {
const canvasRef = useRef<HTMLCanvasElement>(null);
useEffect(() => {
let cancelled = false;
const initApp = async () => {
await SDK3DVerse.joinOrStartSession({
userToken: PUBLIC_TOKEN,
sceneUUID: SCENE_UUID,
canvas: document.getElementById('display-canvas'),
viewportProperties: {
defaultControllerType: SDK3DVerse.controller_type.none,
},
});
};
SDK3DVerse.disconnectFromSession()
.catch(() => null)
.then(async () => {
if (cancelled) return;
await initApp();
});
return () => {
cancelled = true;
};
}, []);
return (
<canvas
ref={canvasRef}
id='display-canvas'
className='w-screen h-screen'
tabIndex={1}
></canvas>
);
};
1
2
3
4
5
6
7
8
9
10
11
useEffect(() => {
const handleEntitiesUpdated = (entities) => {
// Do something with entities
};
SDK3DVerse.notifier.on('onEntitiesUpdated', handleEntitiesUpdated);
return () => {
SDK3DVerse.off('onEntitiesUpdated', handleEntitiesUpdated);
};
}, []);
Previous
Run a template application locally