-
-
Notifications
You must be signed in to change notification settings - Fork 136
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
useCesium
always returns an undefined viewer
#672
Comments
Issue #596 might be related. |
Possible! We aren't using Next or SSR which is why I made is a separate issue |
i have solved it with a strange way. only within the const HandlerComponent = () => {
const { viewer } = useCesium();
const setOsmBuildingsTileset = useSetAtom(atomOsmBuindingsTileset);
console.log(viewer);
useEffect(() => {
createOsmBuildingsAsync().then((osmBuildingsTileset) => {
setOsmBuildingsTileset(osmBuildingsTileset);
viewer?.scene.primitives.add(osmBuildingsTileset);
});
}, []);
return <></>;
};
function App() {
// ANCHOR store
const osmBuildingsTileset = useAtomValue(atomOsmBuindingsTileset);
if (osmBuildingsTileset === undefined) return <>loading...</>;
return (
<>
<Viewer full terrain={terrain} animation={false}>
<Entity position={position} name="Tokyo">
<PointGraphics pixelSize={10} />
<EntityDescription>
<h1>Hello, world.</h1>
<p>JSX is available here!</p>
</EntityDescription>
</Entity>
<CameraLookAt
target={Cartesian3.fromDegrees(144.96007, -37.82249)}
offset={new Cartesian3(0.0, -1500.0, 1200.0)}
/>
<HandlerComponent />
</Viewer>
</>
);
} |
Interesting, makes some sense I guess but still a bit strange, would expect a context value to update regardless of where it's called from. Thanks for the solution! |
|
As a rule, we should use useCesium in Viewer tag. function App() {
const cesiumRef = useRef<CesiumComponentRef<CesiuimViewer>>(null)
const [viewer, setViewer] = useState<Cesium.Viewer>(null)
const getViewer = (): Cesium.Viewer => {
if (!viewer) {
const viewerRef = cesiumRef.current.cesiumElement
setViewer(viewerRef)
return viewerRef
} else {
return viewer
}
}
// get right context by timer
useEffect(() => {
const interval = setInterval(() => {
if (!viewer) {
getViewer()
} else {
clearInterval(interval)
}
}, 100);
return () => {
clearInterval(interval)
}
}, [viewer])
const handleClick = useCallback(() => {
console.log(viewer) // or getViewer(), you will get right property
}, [])
return (
<>
<ResiumViewer ref={cesiumRef} full terrain={terrain} animation={false} onClick={handleClick}>
<Entity position={position} name="Tokyo">
<PointGraphics pixelSize={10} />
<EntityDescription>
<h1>Hello, world.</h1>
<p>JSX is available here!</p>
</EntityDescription>
</Entity>
<CameraLookAt
target={Cartesian3.fromDegrees(144.96007, -37.82249)}
offset={new Cartesian3(0.0, -1500.0, 1200.0)}
/>
</ResiumViewer>
</>
);
} |
Specs:
Expected:
viewer
is defined once the globe has renderedActual:
viewer
is alwaysundefined
, even after the globe and other map layers have renderedRelated: #596
Is there some extra setup needed somewhere to make this properly work?
The text was updated successfully, but these errors were encountered: