React-native-pager-view: Add Tabs connected to ViewPager

Created on 30 Apr 2019  路  8Comments  路  Source: callstack/react-native-pager-view

Should be fairly simple to connect the ViewPager to tabs like is done here: https://github.com/madhu314/react-native-tabbed-view-pager-android

The above library is out of date and it uses a broken version of ViewPager. We could offer tabs as optional and nice to have.

enhancement

Most helpful comment

I just found a way to do it out of the box.

const Tabs = () => {
  const [pageIndex, setPageIndex] = useState(0);

  let viewPager;

  const tabsRef = node => {
    if (node !== null) {
      viewPager = node;
    }
  };

  const setPage = (index: number) => {
    setPageIndex(index);
    viewPager && viewPager.setPage(index);
  };

  const onPageSelected = ({ nativeEvent: { position } }: PageSelectedEvent) => {
    setPageIndex(position);
  };

  return (
    <>
      <ViewPager
        initialPage={0}
        ref={tabsRef}
        onPageSelected={onPageSelected}
      >
        <View key="0">
          <Screen text="A" />
        </View>
        <View key="1">
          <Screen text="B" />
        </View>
      </ViewPager>
      <PageButtons currentIndex={pageIndex} setPage={setPage} />
    </>
  );
};

All 8 comments

Yes this for sure!

It is easier to use and update this library.
https://github.com/ptomasroos/react-native-scrollable-tab-view

I have started to work on this feature, just a proof of concept on Android, will need some help to implement it on iOS 馃槄

I will clean code and create a branch in my fork with those changes and share it (not enough time for that sorry 馃様)

Happy (halloween ||聽"la casta帽era") to everyone!! 馃巸/ 馃尠

reactViewPagerTabs

Oh @x0s3 didn't know somebody was working on it. There is actually a repo that might do the same if they update to use the ViewPager from the community https://github.com/madhu314/react-native-tabbed-view-pager-android/issues/17.

Oh @x0s3 didn't know somebody was working on it. There is actually a repo that might do the same if they update to use the ViewPager from the community madhu314/react-native-tabbed-view-pager-android#17.

it would be useful to close this or update labels, so that there is no confusion in the future 馃槄 (just a comment)

I just found a way to do it out of the box.

const Tabs = () => {
  const [pageIndex, setPageIndex] = useState(0);

  let viewPager;

  const tabsRef = node => {
    if (node !== null) {
      viewPager = node;
    }
  };

  const setPage = (index: number) => {
    setPageIndex(index);
    viewPager && viewPager.setPage(index);
  };

  const onPageSelected = ({ nativeEvent: { position } }: PageSelectedEvent) => {
    setPageIndex(position);
  };

  return (
    <>
      <ViewPager
        initialPage={0}
        ref={tabsRef}
        onPageSelected={onPageSelected}
      >
        <View key="0">
          <Screen text="A" />
        </View>
        <View key="1">
          <Screen text="B" />
        </View>
      </ViewPager>
      <PageButtons currentIndex={pageIndex} setPage={setPage} />
    </>
  );
};

@x0s3 @ferrannp are you still working on it ?

Nope, I haven't had time as https://github.com/madhu314/react-native-tabbed-view-pager-android worked well for me still 馃槄.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

troZee picture troZee  路  3Comments

orcunorcun picture orcunorcun  路  9Comments

ferrannp picture ferrannp  路  10Comments

ivan3177 picture ivan3177  路  6Comments

nnajiabraham picture nnajiabraham  路  6Comments