Is there a backToTop in react-native-navigation?

2019-07-22 07:03发布

When the user hits a tab on a bottom nav bar while already on the screen I'd want to bring the user back to the top of the screen. Anyone know how I can do that using react-native-navigation?

1条回答
淡お忘
2楼-- · 2019-07-22 08:06

Figured it out.

Add this to the page you want to be scrolled up.

 constructor(props) {
    super(props);
    this.props.navigator.setOnNavigatorEvent(this.onNavigatorEvent.bind(this));
  }

If you want more info on setting navigator events then you can check out:

https://wix.github.io/react-native-navigation/#/screen-api?id=listening-to-tab-selected-events

And then add this function:

onNavigatorEvent(event) {
    if (event.id === 'bottomTabSelected') {
      console.log('Tab selected!');
    }
    if (event.id === 'bottomTabReselected') {
      console.log('Tab reselected!');
      this.refs._scrollView.scrollTo({x: 0, y: 0, animated: true});
    }
  }

and add this to your ScrollView:

ref='_scrollView'

Thanks to this:

https://github.com/wix/react-native-navigation/issues/1719

查看更多
登录 后发表回答