Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Prevent ref function from being being called more frequently than needed #1746

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -83,5 +83,36 @@ describe('components/Picker', () => {
const { container } = render(picker);
expect(findSelect(container).value).toBe('22');
});

test('ref function is called when ref changes', () => {
const refMock = jest.fn();
const otherRefMock = jest.fn();

const { rerender } = render(<Picker ref={refMock} />);
expect(refMock).toHaveBeenCalled();

rerender(<Picker ref={otherRefMock} />)
expect(otherRefMock).toHaveBeenCalled();
});

test('ref function is not called every render', () => {
const refMock = jest.fn();

const { rerender } = render(<Picker ref={refMock} />);
expect(refMock).toHaveBeenCalledTimes(1);

rerender(<Picker ref={refMock} />);
expect(refMock).toHaveBeenCalledTimes(1);
});

test('ref function is not called on prop changes', () => {
const refMock = jest.fn();

const { rerender } = render(<Picker ref={refMock} testID={'foo'} />);
expect(refMock).toHaveBeenCalledTimes(1);

rerender(<Picker ref={refMock} testID={'bar'} />);
expect(refMock).toHaveBeenCalledTimes(1);
});
});
});
17 changes: 10 additions & 7 deletions packages/react-native-web/src/exports/Picker/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import setAndForwardRef from '../../modules/setAndForwardRef';
import usePlatformMethods from '../../hooks/usePlatformMethods';
import PickerItem from './PickerItem';
import StyleSheet, { type StyleObj } from '../StyleSheet';
import { forwardRef, useRef } from 'react';
import { forwardRef, useMemo, useRef } from 'react';

type PickerProps = {
...ViewProps,
Expand Down Expand Up @@ -47,12 +47,15 @@ const Picker = forwardRef<PickerProps, *>((props, forwardedRef) => {
} = props;

const hostRef = useRef(null);
const setRef = setAndForwardRef({
getForwardedRef: () => forwardedRef,
setLocalRef: hostNode => {
hostRef.current = hostNode;
}
});
const setRef = useMemo(
() => setAndForwardRef({
getForwardedRef: () => forwardedRef,
setLocalRef: hostNode => {
hostRef.current = hostNode;
},
}),
[forwardedRef]
);

function handleChange(e: Object) {
const { selectedIndex, value } = e.target;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/* eslint-env jasmine, jest */
/* eslint-disable react/jsx-no-bind */

import React from 'react';
import { render } from '@testing-library/react';
import TouchableOpacity from '../';

describe('components/Pressable', () => {
test('ref function is called when ref changes', () => {
const refMock = jest.fn();
const otherRefMock = jest.fn();

const { rerender } = render(<TouchableOpacity ref={refMock} />);
expect(refMock).toHaveBeenCalled();

rerender(<TouchableOpacity ref={otherRefMock} />)
expect(otherRefMock).toHaveBeenCalled();
});

test('ref function is not called every render', () => {
const refMock = jest.fn();

const { rerender } = render(<TouchableOpacity ref={refMock} />);
expect(refMock).toHaveBeenCalledTimes(1);

rerender(<TouchableOpacity ref={refMock} />);
expect(refMock).toHaveBeenCalledTimes(1);
});

test('ref function is not called on prop changes', () => {
const refMock = jest.fn();

const { rerender } = render(<TouchableOpacity ref={refMock} testID={'foo'} />);
expect(refMock).toHaveBeenCalledTimes(1);

rerender(<TouchableOpacity ref={refMock} testID={'bar'} />);
expect(refMock).toHaveBeenCalledTimes(1);
});
});
15 changes: 9 additions & 6 deletions packages/react-native-web/src/exports/Pressable/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,12 +97,15 @@ function Pressable(props: Props, forwardedRef): React.Node {
const [pressed, setPressed] = useForceableState(testOnly_pressed === true);

const hostRef = useRef(null);
const setRef = setAndForwardRef({
getForwardedRef: () => forwardedRef,
setLocalRef: hostNode => {
hostRef.current = hostNode;
}
});
const setRef = useMemo(
() => setAndForwardRef({
getForwardedRef: () => forwardedRef,
setLocalRef: hostNode => {
hostRef.current = hostNode;
}
}),
[forwardedRef]
);

const pressConfig = useMemo(
() => ({
Expand Down
31 changes: 31 additions & 0 deletions packages/react-native-web/src/exports/Text/__tests__/index-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,35 @@ describe('components/Text', () => {
});

test('prop "numberOfLines"', () => {});

test('ref function is called when ref changes', () => {
const refMock = jest.fn();
const otherRefMock = jest.fn();

const { rerender } = render(<Text ref={refMock} />);
expect(refMock).toHaveBeenCalled();

rerender(<Text ref={otherRefMock} />)
expect(otherRefMock).toHaveBeenCalled();
});

test('ref function is not called every render', () => {
const refMock = jest.fn();

const { rerender } = render(<Text ref={refMock} />);
expect(refMock).toHaveBeenCalledTimes(1);

rerender(<Text ref={refMock} />);
expect(refMock).toHaveBeenCalledTimes(1);
});

test('ref function is not called on prop changes', () => {
const refMock = jest.fn();

const { rerender } = render(<Text ref={refMock} testID={'foo'} />);
expect(refMock).toHaveBeenCalledTimes(1);

rerender(<Text ref={refMock} testID={'bar'} />);
expect(refMock).toHaveBeenCalledTimes(1);
});
});
17 changes: 10 additions & 7 deletions packages/react-native-web/src/exports/Text/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
import type { TextProps } from './types';

import * as React from 'react';
import { forwardRef, useContext, useRef } from 'react';
import { forwardRef, useContext, useMemo, useRef } from 'react';
import createElement from '../createElement';
import css from '../StyleSheet/css';
import pick from '../../modules/pick';
Expand Down Expand Up @@ -100,12 +100,15 @@ const Text = forwardRef<TextProps, *>((props, forwardedRef) => {

const hasTextAncestor = useContext(TextAncestorContext);
const hostRef = useRef(null);
const setRef = setAndForwardRef({
getForwardedRef: () => forwardedRef,
setLocalRef: hostNode => {
hostRef.current = hostNode;
}
});
const setRef = useMemo(
() => setAndForwardRef({
getForwardedRef: () => forwardedRef,
setLocalRef: hostNode => {
hostRef.current = hostNode;
}
}),
[forwardedRef]
);

const classList = [
classes.text,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -633,4 +633,35 @@ describe('components/TextInput', () => {
const input = findInput(container);
expect(input.value).toEqual(value);
});

test('ref function is called when ref changes', () => {
const refMock = jest.fn();
const otherRefMock = jest.fn();

const { rerender } = render(<TextInput ref={refMock} />);
expect(refMock).toHaveBeenCalled();

rerender(<TextInput ref={otherRefMock} />)
expect(otherRefMock).toHaveBeenCalled();
});

test('ref function is not called every render', () => {
const refMock = jest.fn();

const { rerender } = render(<TextInput ref={refMock} />);
expect(refMock).toHaveBeenCalledTimes(1);

rerender(<TextInput ref={refMock} />);
expect(refMock).toHaveBeenCalledTimes(1);
});

test('ref function is not called on prop changes', () => {
const refMock = jest.fn();

const { rerender } = render(<TextInput ref={refMock} testID={'foo'} />);
expect(refMock).toHaveBeenCalledTimes(1);

rerender(<TextInput ref={refMock} testID={'bar'} />);
expect(refMock).toHaveBeenCalledTimes(1);
});
});
89 changes: 48 additions & 41 deletions packages/react-native-web/src/exports/TextInput/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

import type { TextInputProps } from './types';

import { forwardRef, useRef } from 'react';
import { forwardRef, useCallback, useMemo, useRef } from 'react';
import createElement from '../createElement';
import css from '../StyleSheet/css';
import pick from '../../modules/pick';
Expand Down Expand Up @@ -188,28 +188,55 @@ const TextInput = forwardRef<TextInputProps, *>((props, forwardedRef) => {

const hostRef = useRef(null);
const dimensions = useRef({ height: null, width: null });
const setRef = setAndForwardRef({
getForwardedRef: () => forwardedRef,
setLocalRef: hostNode => {
// TextInput needs to add more methods to the hostNode in addition to those
// added by `usePlatformMethods`. This is temporarily until an API like
// `TextInput.clear(hostRef)` is added to React Native.
if (hostNode != null) {
hostNode.clear = function() {
if (hostNode != null) {
hostNode.value = '';
}
};
hostNode.isFocused = function() {
return hostNode != null && TextInputState.currentlyFocusedField() === hostNode;
};

const handleContentSizeChange = useCallback(
() => {
const node = hostRef.current;
if (multiline && onContentSizeChange && node != null) {
const newHeight = node.scrollHeight;
const newWidth = node.scrollWidth;
if (newHeight !== dimensions.current.height || newWidth !== dimensions.current.width) {
dimensions.current.height = newHeight;
dimensions.current.width = newWidth;
onContentSizeChange({
nativeEvent: {
contentSize: {
height: dimensions.current.height,
width: dimensions.current.width
}
}
});
}
}
hostRef.current = hostNode;
if (hostRef.current != null) {
handleContentSizeChange();
},
[multiline, onContentSizeChange]
);

const setRef = useMemo(
() => setAndForwardRef({
getForwardedRef: () => forwardedRef,
setLocalRef: hostNode => {
// TextInput needs to add more methods to the hostNode in addition to those
// added by `usePlatformMethods`. This is temporarily until an API like
// `TextInput.clear(hostRef)` is added to React Native.
if (hostNode != null) {
hostNode.clear = function() {
if (hostNode != null) {
hostNode.value = '';
}
};
hostNode.isFocused = function() {
return hostNode != null && TextInputState.currentlyFocusedField() === hostNode;
};
}
hostRef.current = hostNode;
if (hostRef.current != null) {
handleContentSizeChange();
}
}
}
});
}),
[forwardedRef, handleContentSizeChange]
);

function handleBlur(e) {
TextInputState._currentlyFocusedNode = null;
Expand All @@ -219,26 +246,6 @@ const TextInput = forwardRef<TextInputProps, *>((props, forwardedRef) => {
}
}

function handleContentSizeChange() {
const node = hostRef.current;
if (multiline && onContentSizeChange && node != null) {
const newHeight = node.scrollHeight;
const newWidth = node.scrollWidth;
if (newHeight !== dimensions.current.height || newWidth !== dimensions.current.width) {
dimensions.current.height = newHeight;
dimensions.current.width = newWidth;
onContentSizeChange({
nativeEvent: {
contentSize: {
height: dimensions.current.height,
width: dimensions.current.width
}
}
});
}
}
}

function handleChange(e) {
const text = e.target.value;
e.nativeEvent.text = text;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/* eslint-env jasmine, jest */
/* eslint-disable react/jsx-no-bind */

import React from 'react';
import { render } from '@testing-library/react';
import TouchableHighlight from '../';
import View from '../../View';

describe('components/TouchableHighlight', () => {
test('ref function is called when ref changes', () => {
const refMock = jest.fn();
const otherRefMock = jest.fn();

const { rerender } = render(<TouchableHighlight ref={refMock}><View /></TouchableHighlight>);
expect(refMock).toHaveBeenCalled();

rerender(<TouchableHighlight ref={otherRefMock}><View /></TouchableHighlight>)
expect(otherRefMock).toHaveBeenCalled();
});

test('ref function is not called every render', () => {
const refMock = jest.fn();

const { rerender } = render(<TouchableHighlight ref={refMock}><View /></TouchableHighlight>);
expect(refMock).toHaveBeenCalledTimes(1);

rerender(<TouchableHighlight ref={refMock}><View /></TouchableHighlight>);
expect(refMock).toHaveBeenCalledTimes(1);
});

test('ref function is not called on prop changes', () => {
const refMock = jest.fn();

const { rerender } = render(<TouchableHighlight ref={refMock} testID={'foo'}><View /></TouchableHighlight>);
expect(refMock).toHaveBeenCalledTimes(1);

rerender(<TouchableHighlight ref={refMock} testID={'bar'}><View /></TouchableHighlight>);
expect(refMock).toHaveBeenCalledTimes(1);
});
});
Loading