# Hooks ![From v10.0](https://img.shields.io/badge/from-v10.0-green.svg?style=flat-square) Experimental. Components inside [`DeckGL`](deckgl.md) can read its `Deck` and viewports with these hooks. They work on the standalone root and on the [map roots](deckgl.md#map-root-properties), and throw outside `DeckGL`. ```tsx import {useDeck, useViewport, useViewportListener} from '@deck.gl/react'; ``` ## `useViewport` {#useviewport} ```tsx useViewport(viewId?: string): Viewport | null ``` Returns a view's current viewport and re-renders the component when it changes. deck.gl keeps a viewport until the view's camera or size changes, so the component re-renders on camera moves and resizes, not on every frame. * `viewId` - The view's id. Default: the first view. Returns `null` until deck.gl has rendered the view. ```tsx function Labels({data}) { const viewport = useViewport(); // Show labels only when zoomed in return 10} />; } ; ``` ## `useViewportListener` {#useviewportlistener} ```tsx useViewportListener(listener: (viewport: Viewport) => void, viewId?: string): void ``` Calls `listener` with a view's viewport after each frame deck.gl draws in which it changed, without re-rendering the component. Use it to move something on every camera change, such as a map that follows a deck.gl view. It is also called after each render of the component, so a value that arrives later, such as a map from its `onLoad`, gets the current viewport. * `listener` - Receives the viewport. A new function on each render is fine. * `viewId` - The view's id. Default: the first view. ```tsx import {WebMercatorViewport} from '@deck.gl/core'; function FollowCamera({map}: {map: maplibregl.Map | null}) { useViewportListener(viewport => { // Only a map view's viewport has a longitude, latitude, pitch and bearing if (viewport instanceof WebMercatorViewport) { const {longitude, latitude, zoom, pitch, bearing} = viewport; map?.jumpTo({center: [longitude, latitude], zoom, pitch, bearing}); } }, 'main'); return null; } ``` The listener is for updates that are safe to repeat, such as `jumpTo`. It can run more than once with the same viewport: after any render of the component, and twice per camera move when the component also uses `useViewport`. So: * Don't set React state in it. A new value re-renders the component, which calls the listener again, until React stops with "Maximum update depth exceeded". To render from the viewport, use [`useViewport`](#useviewport). * Don't start animations in it, such as `easeTo` or `flyTo`. Each repeated call restarts the animation. * Don't send requests or count events in it, since repeated calls send or count them again. ## `useDeck` {#usedeck} ```tsx useDeck(): InstanceT | null ``` Returns the root's instance and re-renders the component once it exists: the `Deck` on the standalone root, and the overlay on a [map root](deckgl.md#map-root-properties), as [`onDeckChange`](deckgl.md#ondeckchange) and [`ref`](deckgl.md#ref) do. Returns `null` before then. On a map root, pass the overlay's type, such as `useDeck()`. ```tsx function PickOnKey() { const deck = useDeck(); useEffect(() => { if (!deck) return undefined; // Log the object at (100, 100) when P is pressed const onKeyDown = (event: KeyboardEvent) => { if (event.key === 'p') console.log(deck.pickObject({x: 100, y: 100})); }; window.addEventListener('keydown', onKeyDown); return () => window.removeEventListener('keydown', onKeyDown); }, [deck]); return null; } ``` ## Source [modules/react/src/dom/viewport-hooks.ts](https://github.com/visgl/deck.gl/tree/master/modules/react/src/dom/viewport-hooks.ts)