RN: Improve usePressability Return Type (#47596)

Summary:
Pull Request resolved: https://github.com/facebook/react-native/pull/47596

Improves the Flow type of `usePressability` so that if the `config` argument is non-nullable, the return value is non-nullable. This helps reduce unnecessary null checks.

Changelog:
[Internal]

Reviewed By: SamChou19815

Differential Revision: D65908791

fbshipit-source-id: 77de4391d8141d698a5689e617b692b337d01ae5
This commit is contained in:
Tim Yung 2024-11-14 14:07:01 -08:00 committed by Facebook GitHub Bot
parent bea6229a5d
commit 1afde8bd25
4 changed files with 7 additions and 5 deletions

View File

@ -1539,7 +1539,7 @@ function InternalTextInput(props: Props): React.Node {
// TextInput handles onBlur and onFocus events
// so omitting onBlur and onFocus pressability handlers here.
const {onBlur, onFocus, ...eventHandlers} = usePressability(config) || {};
const {onBlur, onFocus, ...eventHandlers} = usePressability(config);
let _accessibilityState;
if (

View File

@ -195,8 +195,7 @@ module.exports = function TouchableWithoutFeedback(props: Props): React.Node {
// BACKWARD-COMPATIBILITY: Focus and blur events were never supported before
// adopting `Pressability`, so preserve that behavior.
const {onBlur, onFocus, ...eventHandlersWithoutBlurAndFocus} =
eventHandlers || {};
const {onBlur, onFocus, ...eventHandlersWithoutBlurAndFocus} = eventHandlers;
const elementProps: {[string]: mixed, ...} = {
...eventHandlersWithoutBlurAndFocus,

View File

@ -14,6 +14,9 @@ import Pressability, {
} from './Pressability';
import {useEffect, useRef} from 'react';
declare function usePressability(config: PressabilityConfig): EventHandlers;
declare function usePressability(config: null | void): null | EventHandlers;
/**
* Creates a persistent instance of `Pressability` that automatically configures
* itself and resets. Accepts null `config` to support lazy initialization. Once
@ -28,7 +31,7 @@ import {useEffect, useRef} from 'react';
*/
export default function usePressability(
config: ?PressabilityConfig,
): ?EventHandlers {
): null | EventHandlers {
const pressabilityRef = useRef<?Pressability>(null);
if (config != null && pressabilityRef.current == null) {
pressabilityRef.current = new Pressability(config);

View File

@ -7233,7 +7233,7 @@ exports[`public API should not change unintentionally Libraries/Pressability/Pre
exports[`public API should not change unintentionally Libraries/Pressability/usePressability.js 1`] = `
"declare export default function usePressability(
config: ?PressabilityConfig
): ?EventHandlers;
): null | EventHandlers;
"
`;