Skip to content

Commit

Permalink
Merge pull request #500 from DataDog/louiszawadzki/fix-webview-ref
Browse files Browse the repository at this point in the history
Forward webview ref
  • Loading branch information
louiszawadzki committed Jun 26, 2023
2 parents 6acdeb9 + bcefddb commit 870422b
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
* Copyright 2016-Present Datadog, Inc.
*/
import { fireEvent, render } from '@testing-library/react-native';
import type { WebView as RNWebView } from 'react-native-webview';
import { NativeModules } from 'react-native';
import React from 'react';

Expand Down Expand Up @@ -51,4 +52,13 @@ describe('WebView', () => {
DdMessage
);
});
it('forwards ref to the actual RN Webview component', async () => {
const ref = React.createRef<RNWebView>();

const { findByTestId } = render(
<WebView testID="webView" allowedHosts={[]} ref={ref} />
);
await findByTestId('webView');
expect(ref.current?.injectJavaScript).toBeDefined();
});
});
7 changes: 5 additions & 2 deletions packages/react-native-webview/src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import type { WebViewMessageEvent, WebViewProps } from 'react-native-webview';
import { WebView as RNWebView } from 'react-native-webview';
import type { TurboModule } from 'react-native';
import { TurboModuleRegistry } from 'react-native';
import React, { useCallback } from 'react';
import React, { forwardRef, useCallback } from 'react';

import {
DATADOG_MESSAGE_PREFIX,
Expand All @@ -19,7 +19,7 @@ type Props = WebViewProps & {
injectedJavaScriptBeforeContentLoaded?: string;
};

export const WebView = (props: Props) => {
const WebViewComponent = (props: Props, ref: React.Ref<RNWebView<Props>>) => {
const userDefinedOnMessage = props.onMessage;
const onMessage = useCallback(
(event: WebViewMessageEvent) => {
Expand All @@ -42,10 +42,13 @@ export const WebView = (props: Props) => {
props.allowedHosts,
props.injectedJavaScriptBeforeContentLoaded
)}
ref={ref}
/>
);
};

export const WebView = forwardRef(WebViewComponent);

export default WebView;

/**
Expand Down

0 comments on commit 870422b

Please sign in to comment.