forked from facebook/react-native
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Pressability: Create
usePressability
Hook
Summary: Creates `usePressability`, a hook to simplify using `Pressability` from functional components. Changelog: [Internal] Reviewed By: yungsters Differential Revision: D18742613 fbshipit-source-id: 55cf48ad60a16a6a5c2c3fa2785f61f784a46b45
- Loading branch information
1 parent
4e31fbd
commit 4bbbe6a
Showing
2 changed files
with
44 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
/** | ||
* 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 Pressability, { | ||
type EventHandlers, | ||
type PressabilityConfig, | ||
} from './Pressability.js'; | ||
import {useEffect, useRef} from 'react'; | ||
|
||
export default function usePressability( | ||
config: PressabilityConfig, | ||
): EventHandlers { | ||
const pressabilityRef = useRef<?Pressability>(null); | ||
if (pressabilityRef.current == null) { | ||
pressabilityRef.current = new Pressability(config); | ||
} | ||
const pressability = pressabilityRef.current; | ||
|
||
// On the initial mount, this is a no-op. On updates, `pressability` will be | ||
// re-configured to use the new configuration. | ||
useEffect(() => { | ||
pressability.configure(config); | ||
}, [config, pressability]); | ||
|
||
// On unmount, reset pending state and timers inside `pressability`. This is | ||
// a separate effect because we do not want to reset when `config` changes. | ||
useEffect(() => { | ||
return () => { | ||
pressability.reset(); | ||
}; | ||
}, [pressability]); | ||
|
||
return pressability.getEventHandlers(); | ||
} |