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 GestureDetector not working correctly with suspense and recycling #2925

Merged
merged 2 commits into from
May 28, 2024

Conversation

j-piasecki
Copy link
Member

@j-piasecki j-piasecki commented May 28, 2024

Description

Fixes #2920

When the subtree containing GestureDetector was suspended, all the native views were unmounted without the Detector component being aware. As a consequence, the view with the attached native recognizer was put in the recycling pool and then reused in other places in the UI (which is bad). Then, when the tree was unsuspended, the gesture didn't work correctly - again, Detector wasn't aware of any change, and a different native view was put as its child so the native recognizer wasn't attached to it.

This PR changes the effect responsible for the initial setup and cleanup to be a layout effect, which gets triggered when the tree is suspended. This means that when tree suspends, native recognizers will be dropped and recreated when the tree unsuspends.

Note that this will only fix the issue on RN 0.74, since useLayoutEffect is broken on versions below that.

Test plan

Tested on the example app and the following code
import {useState} from 'react';
import {Freeze} from 'react-freeze';
import {View, Button, SafeAreaView} from 'react-native';
import {
  Gesture,
  GestureDetector,
  GestureHandlerRootView,
} from 'react-native-gesture-handler';

export default function FirstScreen() {
  const [frozen, setFrozen] = useState(false);
  const tap = Gesture.Tap().onStart(() => {
    console.log('Tap');
  });

  return (
    <GestureHandlerRootView style={{flex: 1}}>
      <SafeAreaView style={{flex: 1, backgroundColor: 'red'}}>
        <Button title="Unfreeze" onPress={() => setFrozen(false)} />
        <Freeze freeze={frozen}>
          <View>
            <GestureDetector gesture={tap}>
              <View
                style={{width: 100, height: 100, backgroundColor: 'blue'}}
              />
            </GestureDetector>
            <View style={{width: 100, height: 100, backgroundColor: 'green'}} />
            <Button title="Freeze" onPress={() => setFrozen(true)} />
          </View>
        </Freeze>
      </SafeAreaView>
    </GestureHandlerRootView>
  );
}

@j-piasecki j-piasecki merged commit 921a9b2 into main May 28, 2024
1 check passed
@j-piasecki j-piasecki deleted the @jpiasecki/fix-suspense-recycling-issue branch May 28, 2024 16:27
github-merge-queue bot referenced this pull request in valora-inc/wallet Jun 26, 2024
…5575)

[![Mend
Renovate](https://app.renovatebot.com/images/banner.svg)](https://renovatebot.com)

This PR contains the following updates:

| Package | Change | Age | Adoption | Passing | Confidence |
|---|---|---|---|---|---|
|
[react-native-gesture-handler](https://togithub.com/software-mansion/react-native-gesture-handler)
| [`^2.16.2` ->
`^2.17.1`](https://renovatebot.com/diffs/npm/react-native-gesture-handler/2.16.2/2.17.1)
|
[![age](https://developer.mend.io/api/mc/badges/age/npm/react-native-gesture-handler/2.17.1?slim=true)](https://docs.renovatebot.com/merge-confidence/)
|
[![adoption](https://developer.mend.io/api/mc/badges/adoption/npm/react-native-gesture-handler/2.17.1?slim=true)](https://docs.renovatebot.com/merge-confidence/)
|
[![passing](https://developer.mend.io/api/mc/badges/compatibility/npm/react-native-gesture-handler/2.16.2/2.17.1?slim=true)](https://docs.renovatebot.com/merge-confidence/)
|
[![confidence](https://developer.mend.io/api/mc/badges/confidence/npm/react-native-gesture-handler/2.16.2/2.17.1?slim=true)](https://docs.renovatebot.com/merge-confidence/)
|

---

### Release Notes

<details>
<summary>software-mansion/react-native-gesture-handler
(react-native-gesture-handler)</summary>

###
[`v2.17.1`](https://togithub.com/software-mansion/react-native-gesture-handler/releases/tag/2.17.1)

[Compare
Source](https://togithub.com/software-mansion/react-native-gesture-handler/compare/2.17.0...2.17.1)

#### 🐛 Bug fixes

- Don't register `checkIntegrityBetweenArchitectures` task when
installed as a dependency by
[@&#8203;j-piasecki](https://togithub.com/j-piasecki) in
[#&#8203;2953](https://togithub.com/software-mansion/react-native-gesture-handler/issues/2953)

**Full Changelog**:
software-mansion/react-native-gesture-handler@2.17.0...2.17.1

###
[`v2.17.0`](https://togithub.com/software-mansion/react-native-gesture-handler/releases/tag/2.17.0)

[Compare
Source](https://togithub.com/software-mansion/react-native-gesture-handler/compare/2.16.2...2.17.0)

#### ❗ Important changes

- Add `ref` property to `Buttons` by
[@&#8203;m-bert](https://togithub.com/m-bert) in
[https://github.com/software-mansion/react-native-gesture-handler/pull/2903](https://togithub.com/software-mansion/react-native-gesture-handler/pull/2903)
- Unify touch events callbacks on `web` with respect to `Android` by
[@&#8203;m-bert](https://togithub.com/m-bert) in
[https://github.com/software-mansion/react-native-gesture-handler/pull/2923](https://togithub.com/software-mansion/react-native-gesture-handler/pull/2923)
- Add `touchType` to `TouchEvent` by
[@&#8203;latekvo](https://togithub.com/latekvo) in
[https://github.com/software-mansion/react-native-gesture-handler/pull/2941](https://togithub.com/software-mansion/react-native-gesture-handler/pull/2941)

#### 👍 Improvements

- Refactor `GestureDetector` by
[@&#8203;j-piasecki](https://togithub.com/j-piasecki) in
[https://github.com/software-mansion/react-native-gesture-handler/pull/2901](https://togithub.com/software-mansion/react-native-gesture-handler/pull/2901)
- Remove lodash by [@&#8203;m-bert](https://togithub.com/m-bert) in
[https://github.com/software-mansion/react-native-gesture-handler/pull/2916](https://togithub.com/software-mansion/react-native-gesture-handler/pull/2916)
- `PointerTracker` refactor. by
[@&#8203;m-bert](https://togithub.com/m-bert) in
[https://github.com/software-mansion/react-native-gesture-handler/pull/2931](https://togithub.com/software-mansion/react-native-gesture-handler/pull/2931)
- Change `offsetX` and `offsetY` calculations in `PointerEventManager`
by [@&#8203;m-bert](https://togithub.com/m-bert) in
[https://github.com/software-mansion/react-native-gesture-handler/pull/2938](https://togithub.com/software-mansion/react-native-gesture-handler/pull/2938)

#### 🐛 Bug fixes

- Make `RootViewGestureHandler` handler cancel awaiting gestures by
[@&#8203;j-piasecki](https://togithub.com/j-piasecki) in
[https://github.com/software-mansion/react-native-gesture-handler/pull/2900](https://togithub.com/software-mansion/react-native-gesture-handler/pull/2900)
- Get right bridge by
[@&#8203;piaskowyk](https://togithub.com/piaskowyk) in
[https://github.com/software-mansion/react-native-gesture-handler/pull/2886](https://togithub.com/software-mansion/react-native-gesture-handler/pull/2886)
- Fix buttons getting stuck after scrolling on them by
[@&#8203;j-piasecki](https://togithub.com/j-piasecki) in
[https://github.com/software-mansion/react-native-gesture-handler/pull/2693](https://togithub.com/software-mansion/react-native-gesture-handler/pull/2693)
- Fix Detector creating and attaching all gestures twice on first mount
by [@&#8203;j-piasecki](https://togithub.com/j-piasecki) in
[https://github.com/software-mansion/react-native-gesture-handler/pull/2914](https://togithub.com/software-mansion/react-native-gesture-handler/pull/2914)
- Fix GestureDetector not working when its children change by
[@&#8203;j-piasecki](https://togithub.com/j-piasecki) in
[https://github.com/software-mansion/react-native-gesture-handler/pull/2921](https://togithub.com/software-mansion/react-native-gesture-handler/pull/2921)
- Fix GestureDetector not working correctly with suspense and recycling
by [@&#8203;j-piasecki](https://togithub.com/j-piasecki) in
[https://github.com/software-mansion/react-native-gesture-handler/pull/2925](https://togithub.com/software-mansion/react-native-gesture-handler/pull/2925)
- Fix nested buttons on the new architecture by
[@&#8203;j-piasecki](https://togithub.com/j-piasecki) in
[https://github.com/software-mansion/react-native-gesture-handler/pull/2926](https://togithub.com/software-mansion/react-native-gesture-handler/pull/2926)
- Unify scaled coordinates between `web` and `native` side. by
[@&#8203;m-bert](https://togithub.com/m-bert) in
[https://github.com/software-mansion/react-native-gesture-handler/pull/2943](https://togithub.com/software-mansion/react-native-gesture-handler/pull/2943)
- \[Web] Send relative coords in event. by
[@&#8203;m-bert](https://togithub.com/m-bert) in
[https://github.com/software-mansion/react-native-gesture-handler/pull/2944](https://togithub.com/software-mansion/react-native-gesture-handler/pull/2944)

#### 🔢 Miscellaneous

- Add information about `GestureHandlerRootView` area and change
component name in quick start section in docs. by
[@&#8203;m-bert](https://togithub.com/m-bert) in
[https://github.com/software-mansion/react-native-gesture-handler/pull/2899](https://togithub.com/software-mansion/react-native-gesture-handler/pull/2899)
- Add Hire us section to docs by
[@&#8203;patrycjakalinska](https://togithub.com/patrycjakalinska) in
[https://github.com/software-mansion/react-native-gesture-handler/pull/2904](https://togithub.com/software-mansion/react-native-gesture-handler/pull/2904)
- Bump ejs from 3.1.7 to 3.1.10 by
[@&#8203;dependabot](https://togithub.com/dependabot) in
[https://github.com/software-mansion/react-native-gesture-handler/pull/2888](https://togithub.com/software-mansion/react-native-gesture-handler/pull/2888)
- Bump tar from 6.2.0 to 6.2.1 in /e2e/web-tests by
[@&#8203;dependabot](https://togithub.com/dependabot) in
[https://github.com/software-mansion/react-native-gesture-handler/pull/2859](https://togithub.com/software-mansion/react-native-gesture-handler/pull/2859)
- docs: add
[@&#8203;swmansion/t-rex-ui](https://togithub.com/swmansion/t-rex-ui) by
[@&#8203;kacperkapusciak](https://togithub.com/kacperkapusciak) in
[https://github.com/software-mansion/react-native-gesture-handler/pull/2895](https://togithub.com/software-mansion/react-native-gesture-handler/pull/2895)
- Change navbar and footer to a reusable component by
[@&#8203;patrycjakalinska](https://togithub.com/patrycjakalinska) in
[https://github.com/software-mansion/react-native-gesture-handler/pull/2902](https://togithub.com/software-mansion/react-native-gesture-handler/pull/2902)
- Add dark mode to button in `Hire us` section by
[@&#8203;patrycjakalinska](https://togithub.com/patrycjakalinska) in
[https://github.com/software-mansion/react-native-gesture-handler/pull/2908](https://togithub.com/software-mansion/react-native-gesture-handler/pull/2908)
- Setup example app to use Expo by
[@&#8203;bohdanprog](https://togithub.com/bohdanprog) in
[https://github.com/software-mansion/react-native-gesture-handler/pull/2905](https://togithub.com/software-mansion/react-native-gesture-handler/pull/2905)
- Revert "docs: add
[@&#8203;swmansion/t-rex-ui](https://togithub.com/swmansion/t-rex-ui)"
by [@&#8203;kacperkapusciak](https://togithub.com/kacperkapusciak) in
[https://github.com/software-mansion/react-native-gesture-handler/pull/2909](https://togithub.com/software-mansion/react-native-gesture-handler/pull/2909)
- Bump [@&#8203;sideway/formula](https://togithub.com/sideway/formula)
from 3.0.0 to 3.0.1 by
[@&#8203;dependabot](https://togithub.com/dependabot) in
[https://github.com/software-mansion/react-native-gesture-handler/pull/2910](https://togithub.com/software-mansion/react-native-gesture-handler/pull/2910)
- docs: add
[@&#8203;swmansion/t-rex-ui](https://togithub.com/swmansion/t-rex-ui) by
[@&#8203;patrycjakalinska](https://togithub.com/patrycjakalinska) in
[https://github.com/software-mansion/react-native-gesture-handler/pull/2911](https://togithub.com/software-mansion/react-native-gesture-handler/pull/2911)
- Fix symlink loop during pods installation by
[@&#8203;j-piasecki](https://togithub.com/j-piasecki) in
[https://github.com/software-mansion/react-native-gesture-handler/pull/2917](https://togithub.com/software-mansion/react-native-gesture-handler/pull/2917)
- Bump rexml from 3.2.6 to 3.2.8 in /FabricExample by
[@&#8203;dependabot](https://togithub.com/dependabot) in
[https://github.com/software-mansion/react-native-gesture-handler/pull/2915](https://togithub.com/software-mansion/react-native-gesture-handler/pull/2915)
- Add animated header to the example app by
[@&#8203;j-piasecki](https://togithub.com/j-piasecki) in
[https://github.com/software-mansion/react-native-gesture-handler/pull/2913](https://togithub.com/software-mansion/react-native-gesture-handler/pull/2913)
- Add option to auto-open last example in the example app by
[@&#8203;j-piasecki](https://togithub.com/j-piasecki) in
[https://github.com/software-mansion/react-native-gesture-handler/pull/2918](https://togithub.com/software-mansion/react-native-gesture-handler/pull/2918)
- Replace current theme components with `@swmansion/t-rex-ui` by
[@&#8203;patrycjakalinska](https://togithub.com/patrycjakalinska) in
[https://github.com/software-mansion/react-native-gesture-handler/pull/2906](https://togithub.com/software-mansion/react-native-gesture-handler/pull/2906)
- Bump rexml from 3.2.6 to 3.2.8 in /MacOSExample by
[@&#8203;dependabot](https://togithub.com/dependabot) in
[https://github.com/software-mansion/react-native-gesture-handler/pull/2924](https://togithub.com/software-mansion/react-native-gesture-handler/pull/2924)
- Bump rexml from 3.2.6 to 3.2.8 in /example by
[@&#8203;dependabot](https://togithub.com/dependabot) in
[https://github.com/software-mansion/react-native-gesture-handler/pull/2930](https://togithub.com/software-mansion/react-native-gesture-handler/pull/2930)
- feat: automatically copy codegen artifacts to paper by
[@&#8203;maciekstosio](https://togithub.com/maciekstosio) in
[https://github.com/software-mansion/react-native-gesture-handler/pull/2933](https://togithub.com/software-mansion/react-native-gesture-handler/pull/2933)
- Add swipeable example rewritten to new API by
[@&#8203;latekvo](https://togithub.com/latekvo) in
[https://github.com/software-mansion/react-native-gesture-handler/pull/2934](https://togithub.com/software-mansion/react-native-gesture-handler/pull/2934)
- Fix new swipeable using useMemo with incomplete dependency list by
[@&#8203;latekvo](https://togithub.com/latekvo) in
[https://github.com/software-mansion/react-native-gesture-handler/pull/2937](https://togithub.com/software-mansion/react-native-gesture-handler/pull/2937)
- docs: update Hire us links to directly lead to contact form by
[@&#8203;kacperkapusciak](https://togithub.com/kacperkapusciak) in
[https://github.com/software-mansion/react-native-gesture-handler/pull/2935](https://togithub.com/software-mansion/react-native-gesture-handler/pull/2935)
- chore(types): add missing pointerType to GestureTouchEvent by
[@&#8203;mgcrea](https://togithub.com/mgcrea) in
[https://github.com/software-mansion/react-native-gesture-handler/pull/2928](https://togithub.com/software-mansion/react-native-gesture-handler/pull/2928)
- Bump braces from 3.0.2 to 3.0.3 in /example by
[@&#8203;dependabot](https://togithub.com/dependabot) in
[https://github.com/software-mansion/react-native-gesture-handler/pull/2945](https://togithub.com/software-mansion/react-native-gesture-handler/pull/2945)
- Bump braces from 3.0.2 to 3.0.3 in /e2e/web-tests by
[@&#8203;dependabot](https://togithub.com/dependabot) in
[https://github.com/software-mansion/react-native-gesture-handler/pull/2946](https://togithub.com/software-mansion/react-native-gesture-handler/pull/2946)
- Bump ws from 6.2.2 to 6.2.3 by
[@&#8203;dependabot](https://togithub.com/dependabot) in
[https://github.com/software-mansion/react-native-gesture-handler/pull/2947](https://togithub.com/software-mansion/react-native-gesture-handler/pull/2947)
- Bump ws from 6.2.2 to 6.2.3 in /e2e/web-tests by
[@&#8203;dependabot](https://togithub.com/dependabot) in
[https://github.com/software-mansion/react-native-gesture-handler/pull/2948](https://togithub.com/software-mansion/react-native-gesture-handler/pull/2948)
- Add `relativeCoords` average to `PointerTracker` by
[@&#8203;m-bert](https://togithub.com/m-bert) in
[https://github.com/software-mansion/react-native-gesture-handler/pull/2939](https://togithub.com/software-mansion/react-native-gesture-handler/pull/2939)
- Add missing import in docs by
[@&#8203;piaskowyk](https://togithub.com/piaskowyk) in
[https://github.com/software-mansion/react-native-gesture-handler/pull/2950](https://togithub.com/software-mansion/react-native-gesture-handler/pull/2950)

#### New Contributors

- [@&#8203;bohdanprog](https://togithub.com/bohdanprog) made their first
contribution in
[https://github.com/software-mansion/react-native-gesture-handler/pull/2905](https://togithub.com/software-mansion/react-native-gesture-handler/pull/2905)
- [@&#8203;maciekstosio](https://togithub.com/maciekstosio) made their
first contribution in
[https://github.com/software-mansion/react-native-gesture-handler/pull/2933](https://togithub.com/software-mansion/react-native-gesture-handler/pull/2933)
- [@&#8203;mgcrea](https://togithub.com/mgcrea) made their first
contribution in
[https://github.com/software-mansion/react-native-gesture-handler/pull/2928](https://togithub.com/software-mansion/react-native-gesture-handler/pull/2928)

**Full Changelog**:
software-mansion/react-native-gesture-handler@2.16.2...2.17.0

</details>

---

### Configuration

📅 **Schedule**: Branch creation - "after 5pm,every weekend" in timezone
America/Los_Angeles, Automerge - "after 5pm,every weekend" in timezone
America/Los_Angeles.

🚦 **Automerge**: Enabled.

♻ **Rebasing**: Whenever PR becomes conflicted, or you tick the
rebase/retry checkbox.

🔕 **Ignore**: Close this PR and you won't be reminded about this update
again.

---

- [ ] <!-- rebase-check -->If you want to rebase/retry this PR, check
this box

---

This PR has been generated by [Mend
Renovate](https://www.mend.io/free-developer-tools/renovate/). View
repository job log
[here](https://developer.mend.io/github/valora-inc/wallet).

<!--renovate-debug:eyJjcmVhdGVkSW5WZXIiOiIzNy40MTMuMiIsInVwZGF0ZWRJblZlciI6IjM3LjQxMy4yIiwidGFyZ2V0QnJhbmNoIjoibWFpbiIsImxhYmVscyI6WyJucG0iLCJyZW5vdmF0ZSJdfQ==-->

---------

Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com>
Co-authored-by: valora-bot <[email protected]>
m-bert added a commit that referenced this pull request Jun 28, 2024
## Description

This PR replaces `queueMicrotask` with `requestAnimationFrame`. Turns out that introducing `useLayoutEffect` (which was introduced in #2925) ended up in gestures being attached in wrong order in some cases. 

This can be observed in #2963 (it is highly recommended to look into repro structure):

1. 3 handlers from `ZoomView` where attached - they have correct `tags`
2. Re-render happens, handlers' `tags` are now `-1`
3. `Pan` from `SVGMask` is attached - all handlers marked as `simultaneous` have `tag` `-1`, therefore relations are not set up
4. Handlers from 1. are attached again, so they get back their original `tags`

In this scenario, `simultaneous handlers` array in `Pan` in `SVGMask` was empty, effectively disabling this relation. Switching to `requestAnimationFrame` solves this problem. 

## Test plan

Verify that examples work as they used to (`draggable`, `multitap`, `transformations`)

Also tested on code from #2963 .
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

GestureDetector is not responsive
2 participants