react-navigation-is-focused-hoc
Welcome React Navigation user seeking focus 😀
This is a quick, ready to use solution using HOC to expose props.isFocused
. No Redux needed
Installation
- Install the latest version of
react-navigation
- Install the latest version of
react-navigation-is-focused-hoc
from npm
yarn add react-navigation-is-focused-hoc
or
npm install --save react-navigation-is-focused-hoc
Full Usage Example
To see more of the react-navigation-is-focused-hoc
in action, you can check out the source in ExampleNavigation folder.
Usage
app.js
// navigationconst AppNavigator = Component { return <AppNavigator onNavigationStateChange= { // If you want to ignore the state changed from `DrawerNavigator`, use this: /* if (/^Drawer(Open|Close|Toggle)$/.test(getCurrentRouteKey(currentState)) === false) { updateFocus(currentState) return } */ } /> }
myScreenView.js
Component static propTypes = isFocused: PropTypesboolisRequired focusedRouteKey: PropTypesstringisRequired { if !thispropsisFocused && nextPropsisFocused // screen enter (refresh data, update ui ...) if thispropsisFocused && !nextPropsisFocused // screen exit } { // Update only once after the screen disappears if thispropsisFocused && !nextPropsisFocused return true // Don't update if the screen is not focused if !thispropsisFocused && !nextPropsisFocused return false // Update the screen if its re-enter return !thispropsisFocused && nextPropsisFocused } { if !thispropsisFocused /*&& this.props.focusedRouteKey.indexOf('Drawer') !== 0*/ return null return <View> thispropsisFocused ? <Text>I am focused</Text> : <Text>I am not focused</Text> </View> } // second argument is optional and is for defining your initial route// ie: export default withNavigationFocus(MyScreenView, true)MyScreenView
Or with ES7 decorators:
@ Component // ...
Contributiors
|
Peter Machowski
|
Patrick Wozniak
|
Charles Crete
|
Ulises Giacoman
Special thanks
Thanks to Adam (@skevy), Mike (@grabbou), Satyajit (@satya164) and others for the react-navigation
module