Skip to content

Commit e4ee330

Browse files
authored
Merge pull request #212 from expo/@sjchmiela/add_ref_support
[js] Add React.Ref support to captureRef
2 parents 4a01cbf + 5c58b2d commit e4ee330

File tree

2 files changed

+9
-6
lines changed

2 files changed

+9
-6
lines changed

src/index.d.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
*/
1010

1111
declare module 'react-native-view-shot' {
12-
import { Component, ReactInstance } from 'react'
12+
import { Component, ReactInstance, RefObject } from 'react'
1313
import { ViewStyle } from 'react-native'
1414

1515
export interface CaptureOptions {
@@ -81,11 +81,11 @@ declare module 'react-native-view-shot' {
8181
/**
8282
* lower level imperative API
8383
*
84-
* @param {React.ReactInstance} viewRef
84+
* @param {React.ReactInstance | RefObject} viewRef
8585
* @param {"react-native-view-shot".CaptureOptions} options
8686
* @return {Promise<string>} Returns a Promise of the image URI.
8787
*/
88-
export function captureRef(viewRef: ReactInstance, options?: CaptureOptions): Promise<string>
88+
export function captureRef<T>(viewRef: ReactInstance | RefObject<T>, options?: CaptureOptions): Promise<string>
8989

9090
/**
9191
* This method release a previously captured uri. For tmpfile it will clean them out, for other result types it

src/index.js

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import React, { Component } from "react";
33
import { View, NativeModules, Platform, findNodeHandle } from "react-native";
44
const { RNViewShot } = NativeModules;
55

6-
import type { Element, ElementRef } from 'react';
6+
import type { Element, ElementRef, ElementType, Ref } from 'react';
77
import type { ViewStyleProp } from 'StyleSheet';
88
import type { LayoutEvent } from 'CoreEventTypes';
99

@@ -88,10 +88,13 @@ function validateOptions(
8888
return { options, errors };
8989
}
9090

91-
export function captureRef(
92-
view: number | ?View,
91+
export function captureRef<T: ElementType>(
92+
view: number | ?View | Ref<T>,
9393
optionsObject?: Object
9494
): Promise<string> {
95+
if (view && typeof view === "object" && "current" in view && view.current) { // React.RefObject
96+
view = view.current;
97+
}
9598
if (typeof view !== "number") {
9699
const node = findNodeHandle(view);
97100
if (!node)

0 commit comments

Comments
 (0)