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

(Android/FlatList) Fix onMomentumScrollEnd being called multiple times #32696

Open
huangkaiyuan opened this issue Dec 2, 2021 · 26 comments
Open

Comments

@huangkaiyuan
Copy link

Description

image

Version

0.64.1

Output of react-native info

image

Steps to reproduce

image

Snack, code example, screenshot, or link to a repository

No response

@fanyangxi
Copy link

Observing the same symptom.
FYI, the onMomentumScrollBegin was triggered only once, but the onMomentumScrollEnd was trigged around 3~5 times.

image

@Arjan-Zuidema
Copy link
Contributor

Arjan-Zuidema commented Dec 15, 2021

Same bug occurs on the ScrollView component

@meowcorp
Copy link

+1

@anton-patrushev
Copy link

Was able to reproduce it on the 0.66.4 version. Android only. Hermes is enabled (maybe it would be useful).
I have faced with that issue on 0.64.2 first time. But it's still reproducible.

@facebook facebook deleted a comment from vamper424 Jan 6, 2022
@rubydeve
Copy link

+1

@rubydeve
Copy link

This is how it worked for me

const [canmomentum, setCanMomentum] = useState(false);
<ScrollView
      onScroll={( event ) => {
           setCanMomentum(true)
     }}
     onMomentumScrollEnd={() => {
           if (canmomentum) console.log('onMomentumScrollEnd')
           setCanMomentum(false)
     }}
>
     <Content/>
</ScrollView>

@ewnavilae
Copy link

This is how it worked for me

const [canmomentum, setCanMomentum] = useState(false);
<ScrollView
      onScroll={( event ) => {
           setCanMomentum(true)
     }}
     onMomentumScrollEnd={() => {
           if (canmomentum) console.log('onMomentumScrollEnd')
           setCanMomentum(false)
     }}
>
     <Content/>
</ScrollView>

Thanks for this suggestion, I did something similar but with a ref instead to avoid unnecessary state updates and renders.

@achiyazigi
Copy link

This is how it worked for me

const [canmomentum, setCanMomentum] = useState(false);
<ScrollView
      onScroll={( event ) => {
           setCanMomentum(true)
     }}
     onMomentumScrollEnd={() => {
           if (canmomentum) console.log('onMomentumScrollEnd')
           setCanMomentum(false)
     }}
>
     <Content/>
</ScrollView>

Thanks for this suggestion, I did something similar but with a ref instead to avoid unnecessary state updates and renders.

I didn't find how to reference the scrolling state from ref. would you agree to share your solution?

@asr1191
Copy link

asr1191 commented Apr 5, 2022

This is how it worked for me

const [canmomentum, setCanMomentum] = useState(false);
<ScrollView
      onScroll={( event ) => {
           setCanMomentum(true)
     }}
     onMomentumScrollEnd={() => {
           if (canmomentum) console.log('onMomentumScrollEnd')
           setCanMomentum(false)
     }}
>
     <Content/>
</ScrollView>

Thanks for this suggestion, I did something similar but with a ref instead to avoid unnecessary state updates and renders.

I didn't find how to reference the scrolling state from ref. would you agree to share your solution?

Second this, please share thy precious knowledge of the hooks. Saveth me from mine own perils! @ewnavilae I beg of you.

@josecarlosrx
Copy link

josecarlosrx commented Apr 20, 2022

Solution using useRef:

const canMomentum = React.useRef(false);

const onMomentumScrollBegin = () => {
  canMomentum.current = true;
};

const onMomentumScrollEnd = () => {
  if (canMomentum.current) {
    // console.log('onMomentumScrollEnd');
  }

  canMomentum.current = false;
};

return (
  <ScrollView
    onMomentumScrollBegin={onMomentumScrollBegin}
    onMomentumScrollEnd={onMomentumScrollEnd}
  ></ScrollView>
);

@MarceloNascc
Copy link

I'm having the same issue in version 0.68.2

@RobbertMerapar
Copy link

Still an issue in version 0.69.6

@MichHighlander
Copy link

Still an issue in version 0.70

@stingerdallas
Copy link

Any update on this? The workaround doesn't work if using the snapToInterval because its the last event that is correct. I can put in a debounce but it is way too long of a debounce since the onMomentumScrollEnd gets called almost a second apart.

When using snapToInterval, and user drags very little and lets go, it snaps back to the original snap location, but onMomentumScrollEnd gets called twice. once for the temp scrolled location and once for the new location

@kirx76
Copy link

kirx76 commented Apr 26, 2023

Still an issue in version 0.71.7

@wyeo
Copy link

wyeo commented May 15, 2023

+1

2 similar comments
@Jlexyc
Copy link

Jlexyc commented Jun 11, 2023

+1

@Bowlerr
Copy link

Bowlerr commented Jun 26, 2023

+1

@gaearon
Copy link
Collaborator

gaearon commented Oct 4, 2023

Looks like this will be fixed by #32433.

@gaearon
Copy link
Collaborator

gaearon commented Oct 4, 2023

Note there's a similar problem for onScrollBeginDrag (which gets called twice). I don't know if that PR fixes it.

@hichemfantar
Copy link

I'm still having this issue in react-native 0.72.6.

@hichemfantar
Copy link

Solution using useRef:

const canMomentum = React.useRef(false);

const onMomentumScrollBegin = () => {
  canMomentum.current = true;
};

const onMomentumScrollEnd = () => {
  if (canMomentum.current) {
    // console.log('onMomentumScrollEnd');
  }

  canMomentum.current = false;
};

return (
  <ScrollView
    onMomentumScrollBegin={onMomentumScrollBegin}
    onMomentumScrollEnd={onMomentumScrollEnd}
  ></ScrollView>
);

This works but the event is called too early, it doesn't wait for the animation to end.

eunbae0 added a commit to uoslife/uoslife-client that referenced this issue Dec 5, 2023
# Key Changes

- Carousel 컴포넌트에서 발생하는 에러를 수정했습니다.

# Details

- Error:
https://uoslife.sentry.io/issues/4689310803/?project=4506321643569152&query=is%3Aunresolved&referrer=issue-stream&statsPeriod=14d&stream_index=6
- Android: [해당
이슈](facebook/react-native#32696 인해 안드로이드에서는
마지막 list에서 스크롤시 처음으로 돌아가는 동작을 제외했습니다.


# Closes Issue

close #209
Copy link

This issue is stale because it has been open 180 days with no activity. Remove stale label or comment or this will be closed in 7 days.

@github-actions github-actions bot added the Stale There has been a lack of activity on this issue and it may be closed soon. label May 17, 2024
@xaiamov
Copy link

xaiamov commented May 22, 2024

It must be fixed in RN 0.74.1 06668fc

@github-actions github-actions bot removed the Stale There has been a lack of activity on this issue and it may be closed soon. label May 23, 2024
@Iuriy-Budnikov
Copy link

Not fixed :(

@mars-lan
Copy link

mars-lan commented Jul 4, 2024

Using RN 0.74.3 and can confirm that this issue has been fixed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests