Skip to content

Instantly share code, notes, and snippets.

@trungpv1601
Last active May 13, 2018 16:41
Show Gist options
  • Save trungpv1601/3e695723821ab440ff07c370750d9ff9 to your computer and use it in GitHub Desktop.
Save trungpv1601/3e695723821ab440ff07c370750d9ff9 to your computer and use it in GitHub Desktop.
React Native: Hide TabBar React Navigation when Keyboard is activated
import React from 'react'
import { Keyboard } from 'react-native'
import { TabBarBottom } from 'react-navigation'

class TabBarComponent extends React.PureComponent {

  constructor(props) {
    super(props)

    this.keyboardWillShow = this.keyboardWillShow.bind(this)
    this.keyboardWillHide = this.keyboardWillHide.bind(this)

    this.state = {
      isVisible: true
    }
  }

  componentWillMount() {
    this.keyboardWillShowSub = Keyboard.addListener('keyboardDidShow', this.keyboardWillShow)
    this.keyboardWillHideSub = Keyboard.addListener('keyboardDidHide', this.keyboardWillHide)
  }

  componentWillUnmount() {
    this.keyboardWillShowSub.remove()
    this.keyboardWillHideSub.remove()
  }

  keyboardWillShow = event => {
    this.setState({
      isVisible: false
    })
  }

  keyboardWillHide = event => {
    this.setState({
      isVisible: true
    })
  }

  render() {
    return this.state.isVisible ?
      <TabBarBottom {...this.props} />
      :
      null
  }
}

export default TabBarComponent

import TabBarComponent from './TabBarComponent.js'

export default TabNavigator({
...
}, {
    initialRouteName: '...',
    tabBarComponent: TabBarComponent,
  })
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment