【问题标题】:React-navigation: How to disable swipe on TabNavigator once init?React-navigation:一旦启动,如何禁用 TabNavigator 上的滑动?
【发布时间】:2017-03-09 15:15:43
【问题描述】:

我像这样设置了TabNavigator:

class RNNavigation extends Component {

  constructor(props) {
    super(props);
  }

  render() {
    const MainScreenNavigator = TabNavigator({
      ProfileStack: { screen: ProfileStack },
      Home: { screen: Home },
      HistoryStack: { screen: HistoryStack }
    }, {
      initialRouteName: 'Home',
      tabBarComponent: () => { return null; },
      swipeEnabled: true
    });

    return (
      <MainScreenNavigator/>
    );
  }
}

有时我需要禁用屏幕组件的滑动。怎么做?是否可以不重新渲染导航器。

------ 2017 年 3 月 15 日编辑

尝试通过 screenProps 传递回调函数(仅适用于 beta 7).. 但正如预期的那样,当我从历史记录中执行此操作时,它会返回到 initialRouteName 'Home',因为它会重新渲染。

redux 也有同样的问题...

欢迎提出任何建议!

【问题讨论】:

    标签: react-native react-navigation


    【解决方案1】:

    你可以把它变成一个变量并创建一个函数来打开/关闭它。

    this.state ={ swipe: true } //set state in constructor
    ....
    toggleSwipe = () => { //function that will be passed down to toggle on/off 
      this.setState((prevState) => ({ swipe: !prevState.swipe });
    }
    ....
    swipeEnabled: this.state.swipe //this will now get toggled between true/false
    

    【讨论】:

    • 是的,但是如何访问屏幕组件内的 toggleSwipe 功能?我尝试使用 screenProps,但由于某种原因,当我编写 时没有呈现任何内容
    • 我认为当您执行 &lt; MainScreenNavigator screenProps={} /&gt; 时不应该渲染任何内容,您只是将 this.props.screenProps 传递给所有选项卡组件。
    • 我的意思是添加 screenProps={...} 就像 doc 说的那样使整个应用程序不呈现。另一个问题,但我的想法是从那里将 ref 传递给 toggleSwipe。您对在屏幕组件中访问函数 toggleSwipe 有何想法?
    • 所以当你在那里添加 screenProps 时它会产生一个空白屏幕?你在里面放了什么?
    • 一个对象,一个字符串,一个数字,同样的效果。我可能在这里误解了一些东西:)
    猜你喜欢
    • 1970-01-01
    • 2018-06-15
    • 2020-07-09
    • 2018-11-12
    • 1970-01-01
    • 1970-01-01
    • 2018-12-28
    • 2021-11-02
    • 1970-01-01
    相关资源
    最近更新 更多