Long press hook
  • Introduction
  • Installation
  • Basics
  • Advanced
    • Definition
    • Callback
    • Options
      • Configuration
        • Events detection
        • Long press threshold
      • React events
        • Persisting event
        • Filtering events
      • Callbacks
        • Press started
        • On move
        • Press finished
        • Long press cancelled
      • Cancellation
        • Cancel outside element
        • Cancel on movement
    • Hook result / Bind function
      • Context
      • Handlers
  • Examples
    • Advanced usage example
    • Live examples
      • Version 3
      • Version 2 (deprecated)
      • Version 1 (deprecated)
  • Migration
    • V1 to V2
    • V2 to V3
    • ⚠️V3.1+
    • Change log
  • Contributions
    • ❤️Donations
    • Pull requests policy
  • Architectural Decisions
    • V1 and V2 deprecation
    • Moving to new repository
Powered by GitBook
On this page
  • Context changes during component lifecycle
  • Context value when long press finish outside element

Was this helpful?

  1. Advanced
  2. Hook result / Bind function

Context

PreviousHook result / Bind functionNextHandlers

Last updated 1 year ago

Was this helpful?

You can supply custom context to the bind function like bind(context) and then access it from callbacks (, , , , ) second argument e.g.: onStart: (event, { context }) => ...

Context changes during component lifecycle

All callbacks but long press callback will use latest provided context. callback will receive context as it was when long press started.

To better understand how component updates affect context let's analyze example below

  1. Initial render

import { useLongPress } from "./use-long-press";

const MyComponent: FC = () => {
  const bind = useLongPress();

  return <div {...bind('context 1')}/>;
}
  1. Div is clicked, triggering onStart with 'context 1'

  2. Context changed to 'context 2'

// ...
<div {...bind('context 2')}/>
// ...
  1. threshold time elapses triggering callback with 'context 1' as it was when long press started

  2. Click finish triggering onFinish with 'context 2' since it changed

Context value when long press finish outside element

When cancelOutsideElement option is set to false long press finish will be detected on window therefore it won't be possible to determine which context was used.

Let's say we use ...bind('test'), therefore onStart and callback will receive context 'test' but onCancel or onFinish if triggered outside element will receive context undefined

onStart
callback
onFinish
onCancel
onMove