callstack / react-native-pager-view

React Native wrapper for the Android ViewPager and iOS UIPageViewController.
MIT License
2.71k stars 417 forks source link

UseRef in android does not setPage if route changes (works in IOS) #816

Open AyoCodess opened 7 months ago

AyoCodess commented 7 months ago

Im using expo router v.3. I’m accessing the ref via my global state manager in another component (screen header). Everything works in IOS as expected, but in android if i replace the route, the ref becomes null immediately and the page stays on the current page.

const showDialog = () => {
    Alert.alert(
      'Hey Ayo!',
      'Are you sure you want to go back to home. You will loose all current progress.',
      [
        {
          text: 'Cancel',
          onPress: () => console.log('Cancel Pressed'),
          style: 'cancel'
        },
        {
          text: 'Go Home',
          onPress: () => {
            setCurrentMomentState({
              selectedDate: 'none',
              selectedMomentType: 'none'
            });
            (pagerViewRef.current as any).setPage(0);

            router.replace('/(protected)/(tabs)/home');
          }
        }
      ]
    );

Using a set-time around the set page call on android is a hacky solution.

Beat-YT commented 6 months ago

I have the same issue

AyoCodess commented 6 months ago

I have the same issue

For now the only workaround is using a setTimeout.