satya164 / react-native-tab-view

A cross-platform Tab View component for React Native
MIT License
5.13k stars 1.07k forks source link

iOS crashes if index in navigationState is updated too quickly #1344

Closed jasonkv closed 2 years ago

jasonkv commented 2 years ago

Current behavior

On iOS (Android works fine) there is a hard crash (no logs) that happens if index in navigationState is updated too quickly.

This happens when a user might press a button to switch to the next tab too quickly (multiple times in a row):

Example code to reproduce (roughly):


import * as React from 'react';
import { View, useWindowDimensions, Button } from 'react-native';
import { TabView, SceneMap } from 'react-native-tab-view';

const FirstRoute = () => (
  <View style={{ flex: 1, backgroundColor: '#ff4081' }} />
);

const SecondRoute = () => (
  <View style={{ flex: 1, backgroundColor: '#673ab7' }} />
);

const renderScene = SceneMap({
  0: FirstRoute,
  1: SecondRoute,

  2: FirstRoute,
  3: SecondRoute,
  /* ... a few more entries */
});

export default function TabViewExample() {
  const layout = useWindowDimensions();

  const [index, setIndex] = React.useState(0);
  const [routes] = React.useState([
    { key: 0, title: 'First' },
    { key: 1, title: 'Second' },
    { key: 3, title: 'Third' },
    { key: 4, title: 'Fourth' },
      /* ... a few more entries */

  ]);
  const routesCount = routes?.length;
  return (<View>

    <Button 
        onPress={() => setIndex(Math.max(index - 1, 0))} 
        disabled={index === 0}
      >
        Backwards
      </Button>
      <Button 
        onPress={() => setIndex(Math.min(index + 1, routesCount - 1)} 
        disabled={index === routesCount - 1}
      >
          Forward
      </Button>

        <TabView
          navigationState={{ index, routes }}
          renderScene={renderScene}
          onIndexChange={setIndex}
          initialLayout={{ width: layout.width }}
        />
    </View>
  );
}

The problem seems to be in a library react-native-pager-view used inside PagerViewAdapter. This component calls: pagerRef.current?.setPage(index);

Which if switched to: pagerRef.current?.setPageWithoutAnimation(index);

With this change, it seems to work fine but lacks the animation.

Note: There is no crash if navigating through the tabs via quickly swiping instead of pressing the buttons to navigate through the tabs.

Expected behavior

For it to not crash.

Reproduction

Example Provided above

Platform

Environment

package version
react-native-tab-view ^3.1.1
react-native-pager-view 5.4.9
react-native 0.64.3
expo ~44.0.0
node
npm or yarn
github-actions[bot] commented 2 years ago

Hey! Thanks for opening the issue. The issue doesn't seem to contain a link to a repro (a snack.expo.dev link or link to a GitHub repo under your username).

Can you provide a minimal repro which demonstrates the issue? A repro will help us debug the issue faster. Please try to keep the repro as small as possible and make sure that we can run it without additional setup.

github-actions[bot] commented 2 years ago

The versions mentioned in the issue for the following packages differ from the latest versions on npm:

Can you verify that the issue still exists after upgrading to the latest versions of these packages?

github-actions[bot] commented 2 years ago

Hello 👋, this issue has been open for more than a month without a repro or any activity. If the issue is still present in the latest version, please provide a repro or leave a comment within 7 days to keep it open, otherwise it will be closed automatically. If you found a solution or workaround for the issue, please comment here for others to find. If this issue is critical for you, please consider sending a pull request to fix it.

nitin-jotwani-fc commented 2 years ago

getting same issue in ios for version 3.1.1

github-actions[bot] commented 2 years ago

Hello 👋, this issue has been open for more than a month without a repro or any activity. If the issue is still present in the latest version, please provide a repro or leave a comment within 7 days to keep it open, otherwise it will be closed automatically. If you found a solution or workaround for the issue, please comment here for others to find. If this issue is critical for you, please consider sending a pull request to fix it.

BANG88 commented 2 years ago

Hello 👋, this issue has been open for more than a month without a repro or any activity. If the issue is still present in the latest version, please provide a repro or leave a comment within 7 days to keep it open, otherwise it will be closed automatically. If you found a solution or workaround for the issue, please comment here for others to find. If this issue is critical for you, please consider sending a pull request to fix it.

JOKE.

We need a solution for the issue.