Skip to content

Commit 587a876

Browse files
fix(react): reactive hooks, expose type def, simplify logic (#329)
* chore(deps): bump modules and examples to 2.0.0-alpha.3 * chore: update lockfile * chore(deps): bump modules and examples to 2.0.0-alpha.3 * chore: update lockfile * fix: update hubble hooks when params change * fix: expose FormatConfig * refactor: simplify useHubbleGl hook return value * Actualizar hooks.ts Co-authored-by: Chris Gervang <[email protected]> --------- Co-authored-by: Chris Gervang <[email protected]>
1 parent 22f0d5b commit 587a876

File tree

2 files changed

+25
-33
lines changed

2 files changed

+25
-33
lines changed

modules/main/src/index.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,9 @@ export {
2929
AnimationManager,
3030
Animation,
3131
DeckAnimation,
32-
KeplerAnimation
32+
KeplerAnimation,
33+
// Config
34+
FormatConfigs
3335
} from '@hubble.gl/core';
3436

3537
export {

modules/react/src/hooks.ts

Lines changed: 22 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,12 @@ export function useDeckAdapter(
2727
a.animationManager.attachAnimation(deckAnimation);
2828
deckAnimation.draw();
2929
return a;
30-
}, []);
30+
}, [deckAnimation]);
3131
return {adapter, layers, cameraFrame, setCameraFrame};
3232
}
3333

3434
export function useDeckAnimation(params: DeckAnimationConstructor) {
35-
return useMemo(() => new DeckAnimation(params), []);
35+
return useMemo(() => new DeckAnimation(params), [params]);
3636
}
3737

3838
export function useHubbleGl<ReactMapRef extends MapRef>({
@@ -53,43 +53,33 @@ export function useHubbleGl<ReactMapRef extends MapRef>({
5353
initialViewState
5454
);
5555

56-
const onMapLoad = useCallback(() => {
57-
if (mapRef) {
58-
const map = mapRef.current.getMap();
59-
map.on('render', () => adapter.onAfterRender(nextFrame, map.areTilesLoaded()));
60-
}
61-
}, [adapter, nextFrame]);
62-
63-
if (!mapRef) {
56+
const mapProps = useMemo(() => {
57+
if (!mapRef) return {};
6458
return {
65-
adapter,
66-
cameraFrame,
67-
setCameraFrame,
68-
mapProps: {},
69-
deckProps: adapter.getProps({
70-
deck,
71-
onNextFrame: nextFrame,
72-
extraProps: {
73-
layers
59+
onLoad: () => {
60+
if (mapRef.current) {
61+
const map = mapRef.current.getMap();
62+
map.on('render', () => adapter.onAfterRender(nextFrame, map.areTilesLoaded()));
7463
}
75-
})
64+
},
65+
preventStyleDiffing: true
7666
};
77-
}
67+
}, [mapRef, adapter, nextFrame]);
68+
69+
const deckProps = useMemo(() => {
70+
if (!deck) return {};
71+
return adapter.getProps({
72+
deck,
73+
onNextFrame: mapRef ? undefined : nextFrame,
74+
extraProps: {layers}
75+
});
76+
}, [deck, adapter, layers, mapRef, nextFrame]);
7877

7978
return {
8079
adapter,
8180
cameraFrame,
8281
setCameraFrame,
83-
onMapLoad,
84-
mapProps: {
85-
onLoad: onMapLoad,
86-
preventStyleDiffing: true
87-
},
88-
deckProps: adapter.getProps({
89-
deck,
90-
extraProps: {
91-
layers
92-
}
93-
})
82+
mapProps,
83+
deckProps
9484
};
9585
}

0 commit comments

Comments
 (0)