触发器类功能从选项卡导航器选项卡调用按 - 反应原生/导航

时间:2017-11-21 20:45:37

标签: javascript react-native tabs react-navigation

我有一个TabNavigator组件,我希望单个选项卡屏幕状态在选项卡上重置。如何在选项卡上调用类(组件)方法?我查看了tabBarOnPress,但是它从父级别(TabNavigator)获取了一个函数。我希望触发类方法,我不想导出该方法。

1 个答案:

答案 0 :(得分:3)

假设你有像这样的TabNavigator

const HOME = TabNavigator({
  [SCREENS.EXPLORESCREEN]: { screen: ExploreScreen },
  [SCREENS.ASKSCREEN]: { screen: AskScreen },
  },{
        initialRouteName: SCREENS.EXPLORESCREEN
    })
  const PrimaryNav = StackNavigator({
  [SCREENS.HOMESCREEN]: {
    screen: HOME,
    navigationOptions=({navigation})=>{
    return{
          tabBarOnPress :()=>{ 
            if (typeof navigation.state.params.cancelState!=="undefined"){
                 navigation.state.params.cancelState()
            }
            else{
            //your stuff
            }
           }   
      }
    }
  },
},
  {
    // Default config for all screens
    headerMode: "screen",
    initialRouteName: SCREENS.HOMESCREEN
    ,

  }

);

ExploreScreen.js

class ExploreScreen extends Component {

  componentDidMount(){
    this.props.navigation.setParams({
    cancelState:this.cancelState
    })

  }
  cancelState=()=>{
  //your Stuff
  }
}
  • 您也可以在组件级别使用navigationOptions。