All files / react-native-web/src/modules/usePressEvents index.js

100% Statements 11/11
100% Branches 2/2
100% Functions 4/4
100% Lines 11/11

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42                                    35x 35x 26x   35x     35x 26x         35x 26x 26x       35x   35x    
/**
 * Copyright (c) Facebook, Inc. and its affiliates.
 *
 * This source code is licensed under the MIT license found in the
 * LICENSE file in the root directory of this source tree.
 *
 * @flow strict-local
 * @format
 */
 
'use strict';
 
import type { EventHandlers, PressResponderConfig } from './PressResponder';
 
import PressResponder from './PressResponder';
import { useDebugValue, useEffect, useRef } from 'react';
 
export default function usePressEvents(hostRef: any, config: PressResponderConfig): EventHandlers {
  const pressResponderRef = useRef<?PressResponder>(null);
  if (pressResponderRef.current == null) {
    pressResponderRef.current = new PressResponder(config);
  }
  const pressResponder = pressResponderRef.current;
 
  // Re-configure to use the current node and configuration.
  useEffect(() => {
    pressResponder.configure(config);
  }, [config, pressResponder]);
 
  // Reset the `pressResponder` when cleanup needs to occur. This is
  // a separate effect because we do not want to rest the responder when `config` changes.
  useEffect(() => {
    return () => {
      pressResponder.reset();
    };
  }, [pressResponder]);
 
  useDebugValue(config);
 
  return pressResponder.getEventHandlers();
}