Skip to content

Commit 6668fe0

Browse files
Fix MapboxLayer React integration (visgl#6053)
1 parent 5f863f6 commit 6668fe0

File tree

1 file changed

+26
-25
lines changed

1 file changed

+26
-25
lines changed

modules/react/src/deckgl.js

Lines changed: 26 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ const DeckGL = forwardRef((props, ref) => {
9494
const _thisRef = useRef({});
9595
const thisRef = _thisRef.current;
9696
// A mechanism to force redraw
97-
const [, setVersion] = useState(0);
97+
const [version, setVersion] = useState(0);
9898
thisRef.forceUpdate = setVersion;
9999
// DOM refs
100100
const containerRef = useRef(null);
@@ -137,32 +137,35 @@ const DeckGL = forwardRef((props, ref) => {
137137
// Update Deck's props. If Deck needs redraw, this will trigger a call to `_customRender` in
138138
// the next animation frame.
139139
// Needs to be called both from initial mount, and when new props are received
140-
const updateFromProps = () => {
141-
if (!thisRef.deck) {
142-
return;
143-
}
144-
const deckProps = {
145-
...props,
146-
// Override user styling props. We will set the canvas style in render()
147-
style: null,
148-
width: '100%',
149-
height: '100%',
150-
layers: jsxProps.layers,
151-
views: jsxProps.views,
152-
onViewStateChange: handleViewStateChange,
153-
onInteractionStateChange: handleInteractionStateChange
154-
};
155-
156-
thisRef.deck.setProps(deckProps);
157-
};
140+
const deckProps = useMemo(
141+
() => {
142+
const forwardProps = {
143+
...props,
144+
// Override user styling props. We will set the canvas style in render()
145+
style: null,
146+
width: '100%',
147+
height: '100%',
148+
layers: jsxProps.layers,
149+
views: jsxProps.views,
150+
onViewStateChange: handleViewStateChange,
151+
onInteractionStateChange: handleInteractionStateChange
152+
};
153+
154+
if (thisRef.deck) {
155+
thisRef.deck.setProps(forwardProps);
156+
}
157+
158+
return forwardProps;
159+
},
160+
[props]
161+
);
158162

159163
useEffect(() => {
160164
thisRef.deck = createDeckInstance(thisRef, {
161-
...props,
165+
...deckProps,
162166
parent: containerRef.current,
163167
canvas: canvasRef.current
164168
});
165-
updateFromProps();
166169

167170
return () => thisRef.deck.finalize();
168171
}, []);
@@ -185,8 +188,6 @@ const DeckGL = forwardRef((props, ref) => {
185188

186189
useImperativeHandle(ref, () => getRefHandles(thisRef), []);
187190

188-
updateFromProps();
189-
190191
const {viewManager} = thisRef.deck || {};
191192
const currentViewports = viewManager && viewManager.getViewports();
192193

@@ -205,11 +206,11 @@ const DeckGL = forwardRef((props, ref) => {
205206
// before Deck redraw to ensure perfect synchronization & avoid excessive redraw
206207
// This is because multiple changes may happen to Deck between two frames e.g. transition
207208
if (
208-
!thisRef.control || // initial mount
209209
(!thisRef.viewStateUpdateRequested && thisRef.lastRenderedViewports === currentViewports) || // case 2
210-
thisRef.redrawReason // case 3 just before deck redraws
210+
thisRef.version !== version // case 3 just before deck redraws
211211
) {
212212
thisRef.lastRenderedViewports = currentViewports;
213+
thisRef.version = version;
213214

214215
// Render the background elements (typically react-map-gl instances)
215216
// using the view descriptors

0 commit comments

Comments
 (0)