Skip to content

Commit 6e44878

Browse files
committed
msglist: Tweak how handleMessage callback gets the props
1 parent dd81220 commit 6e44878

File tree

1 file changed

+27
-14
lines changed

1 file changed

+27
-14
lines changed

src/webview/MessageList.js

Lines changed: 27 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -173,20 +173,6 @@ function MessageListInner(props: Props) {
173173
[],
174174
);
175175

176-
const handleMessage = React.useCallback(
177-
(event: { +nativeEvent: { +data: string, ... }, ... }) => {
178-
const eventData: WebViewOutboundEvent = JSON.parse(event.nativeEvent.data);
179-
if (eventData.type === 'ready') {
180-
sendInboundEventsIsReady.current = true;
181-
sendInboundEvents([{ type: 'ready' }, ...unsentInboundEvents.current]);
182-
unsentInboundEvents.current = [];
183-
} else {
184-
handleWebViewOutboundEvent(props, eventData);
185-
}
186-
},
187-
[props, sendInboundEvents],
188-
);
189-
190176
const propsRef = React.useRef(props);
191177
React.useEffect(() => {
192178
const lastProps = propsRef.current;
@@ -206,6 +192,33 @@ function MessageListInner(props: Props) {
206192
}
207193
}, [props, sendInboundEvents]);
208194

195+
const handleMessage = React.useCallback(
196+
(event: { +nativeEvent: { +data: string, ... }, ... }) => {
197+
const eventData: WebViewOutboundEvent = JSON.parse(event.nativeEvent.data);
198+
if (eventData.type === 'ready') {
199+
sendInboundEventsIsReady.current = true;
200+
sendInboundEvents([{ type: 'ready' }, ...unsentInboundEvents.current]);
201+
unsentInboundEvents.current = [];
202+
} else {
203+
// Instead of closing over `props` itself, we indirect through
204+
// `propsRef`, which gets updated by the effect above.
205+
//
206+
// That's because unlike in a typical component, the UI this acts as
207+
// a UI callback for isn't based on the current props, but on the
208+
// data we've communicated through inbound-events. (See discussion
209+
// at top of component.) So that's the data we want to refer to
210+
// when interpreting the user's interaction; and `propsRef` is what
211+
// the effect above updates in sync with sending those events.
212+
//
213+
// (The distinction may not matter much here in practice. But a
214+
// nice bonus of this way is that we avoid re-renders of
215+
// SinglePageWebView, potentially a helpful optimization.)
216+
handleWebViewOutboundEvent(propsRef.current, eventData);
217+
}
218+
},
219+
[sendInboundEvents],
220+
);
221+
209222
// We compute the page contents as an HTML string just once, on this
210223
// MessageList's first render. See discussion at top of function.
211224
//

0 commit comments

Comments
 (0)