Is there a backToTop in react-native-navigation?

时间秒杀一切 提交于 2019-12-11 08:51:04

问题


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:


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



来源:https://stackoverflow.com/questions/51144588/is-there-a-backtotop-in-react-native-navigation

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!