diff --git a/packages/react-navigation/examples/NavigationPlayground/js/App.js b/packages/react-navigation/examples/NavigationPlayground/js/App.js
index bf27dd5c..4769706b 100644
--- a/packages/react-navigation/examples/NavigationPlayground/js/App.js
+++ b/packages/react-navigation/examples/NavigationPlayground/js/App.js
@@ -109,10 +109,10 @@ const ExampleInfo = {
name: 'Animated Tabs Example',
description: 'Tab transitions have custom animations',
},
- // TabsWithNavigationFocus: {
- // name: 'withNavigationFocus',
- // description: 'Receive the focus prop to know when a screen is focused',
- // },
+ TabsWithNavigationFocus: {
+ name: 'withNavigationFocus',
+ description: 'Receive the focus prop to know when a screen is focused',
+ },
};
const ExampleRoutes = {
@@ -139,8 +139,8 @@ const ExampleRoutes = {
screen: SimpleTabs,
path: 'settings',
},
- TabAnimations: TabAnimations,
- // TabsWithNavigationFocus: TabsWithNavigationFocus,
+ TabAnimations,
+ TabsWithNavigationFocus,
};
type State = {
diff --git a/packages/react-navigation/examples/NavigationPlayground/js/TabsWithNavigationFocus.js b/packages/react-navigation/examples/NavigationPlayground/js/TabsWithNavigationFocus.js
index a2f0df26..f957e2dd 100644
--- a/packages/react-navigation/examples/NavigationPlayground/js/TabsWithNavigationFocus.js
+++ b/packages/react-navigation/examples/NavigationPlayground/js/TabsWithNavigationFocus.js
@@ -3,40 +3,75 @@
*/
import React from 'react';
-import { SafeAreaView, Text } from 'react-native';
+import { Button, SafeAreaView, Text } from 'react-native';
import { TabNavigator, withNavigationFocus } from 'react-navigation';
import MaterialCommunityIcons from 'react-native-vector-icons/MaterialCommunityIcons';
import SampleText from './SampleText';
-const createTabScreen = (name, icon, focusedIcon, tintColor = '#673ab7') => {
- const TabScreen = ({ isFocused }) => (
-
-
- {'Tab ' + name.toLowerCase()}
+class Child extends React.Component {
+ render() {
+ return (
+
+ {this.props.isFocused
+ ? 'I know that my parent is focused!'
+ : 'My parent is not focused! :O'}
- {'props.isFocused: ' + (isFocused ? ' true' : 'false')}
-
- );
+ );
+ }
+}
- TabScreen.navigationOptions = {
- tabBarLabel: name,
- tabBarIcon: ({ tintColor, focused }) => (
-
- ),
- };
+const ChildWithNavigationFocus = withNavigationFocus(Child);
+const createTabScreen = (name, icon, focusedIcon, tintColor = '#673ab7') => {
+ class TabScreen extends React.Component {
+ static navigationOptions = {
+ tabBarLabel: name,
+ tabBarIcon: ({ tintColor, focused }) => (
+
+ ),
+ };
+
+ state = { showChild: false };
+
+ render() {
+ const { isFocused } = this.props;
+
+ return (
+
+
+ {'Tab ' + name.toLowerCase()}
+
+
+ {'props.isFocused: ' + (isFocused ? ' true' : 'false')}
+
+ {this.state.showChild ? (
+
+ ) : (
+
+ );
+ }
+ }
return withNavigationFocus(TabScreen);
};
diff --git a/packages/react-navigation/src/navigators/createNavigator.js b/packages/react-navigation/src/navigators/createNavigator.js
index 0d64993a..0255ca32 100644
--- a/packages/react-navigation/src/navigators/createNavigator.js
+++ b/packages/react-navigation/src/navigators/createNavigator.js
@@ -26,6 +26,12 @@ function createNavigator(NavigatorView, router, navigationConfig) {
Object.values(this.childEventSubscribers).map(s => s.removeAll());
}
+ _isRouteFocused = route => () => {
+ const { state } = this.props.navigation;
+ const focusedRoute = state.routes[state.index];
+ return route === focusedRoute;
+ };
+
render() {
const { navigation, screenProps } = this.props;
const { dispatch, state, addListener } = navigation;
@@ -47,6 +53,7 @@ function createNavigator(NavigatorView, router, navigationConfig) {
dispatch,
state: route,
addListener: this.childEventSubscribers[route.key].addListener,
+ isFocused: this._isRouteFocused.bind(this, route),
});
const options = router.getScreenOptions(childNavigation, screenProps);
descriptors[route.key] = {
diff --git a/packages/react-navigation/src/views/withNavigationFocus.js b/packages/react-navigation/src/views/withNavigationFocus.js
index 59217627..98eb3d5d 100644
--- a/packages/react-navigation/src/views/withNavigationFocus.js
+++ b/packages/react-navigation/src/views/withNavigationFocus.js
@@ -12,9 +12,13 @@ export default function withNavigationFocus(Component) {
navigation: propTypes.object.isRequired,
};
- state = {
- isFocused: false,
- };
+ constructor(props, context) {
+ super();
+
+ this.state = {
+ isFocused: this.getNavigation(props, context).isFocused(),
+ };
+ }
componentDidMount() {
const navigation = this.getNavigation();
@@ -32,8 +36,8 @@ export default function withNavigationFocus(Component) {
this.subscriptions.forEach(sub => sub.remove());
}
- getNavigation = () => {
- const navigation = this.props.navigation || this.context.navigation;
+ getNavigation = (props = this.props, context = this.context) => {
+ const navigation = props.navigation || context.navigation;
invariant(
!!navigation,
'withNavigationFocus can only be used on a view hierarchy of a navigator. The wrapped component is unable to get access to navigation from props or context.'