This set of enhancers exposes the hooks from react navigation to the bach compose chain.
npm install @truefit/bach-react-navigation @truefit/bach
or
yarn add @truefit/bach-react-navigation @truefit/bach
Sometimes we want to run side-effects when a screen is focused. A side effect may involve things like adding an event listener, fetching data, updating document title, etc. While this can be achieved using focus and blur events, it's not very ergonomic.
Example
import React from 'react';
import {compose} from '@truefit/bach';
import {withFocusEffect} from '@truefit/bach-react-navigation';
import {View, Text} from 'react-native';
const Component = () => (
<View>
<Text>
Hello World
</Text>
</View>
);
export default compose(
withFocusEffect(() => {
console.log('Hello World');
}),
)(Component);
import React from 'react';
import {compose} from '@truefit/bach';
import {withFocusEffect} from '@truefit/bach-react-navigation';
import {View, Text} from 'react-native';
const Component = () => (
<View>
<Text>
Hello World
</Text>
</View>
);
export default compose(
withFocusEffect(() => {
console.log('Hello World');
}),
)(Component);
React Navigation Hook useFocusEffect
We might want to render different content based on the current focus state of the screen.
Example
import React from 'react';
import {compose} from '@truefit/bach';
import {withIsFocused, IsFocused} from '@truefit/bach-react-navigation';
import {View, Text} from 'react-native';
type Props = {
} & IsFocused;
const Component = ({isFocused}: Props) => (
<View>
<Text>
{isFocused ? 'Hi' : 'Shhh'}
</Text>
</View>
);
export default compose(
withIsFocused()
)(Component);
import React from 'react';
import {compose} from '@truefit/bach';
import {withIsFocused} from '@truefit/bach-react-navigation';
import {View, Text} from 'react-native';
const Component = ({isFocused}) => (
<View>
<Text>
{isFocused ? 'Hi' : 'Shhh'}
</Text>
</View>
);
export default compose(
withIsFocused()
)(Component);
React Navigation Hook useIsFocused
Lets us handle deep links in our apps.
React Navigation Hook useLinking
Gives access to navigation object. It's useful when you cannot pass the navigation prop into the component directly, or don't want to pass it in case of a deeply nested child.
Example
import React from 'react';
import {compose} from '@truefit/bach';
import {withNavigation} from '@truefit/bach-react-navigation';
import {NavigationProp} from '<your navigation prop>';
import {View, Button} from 'react-native';
type Props = {
navigation: NavigationProp;
handeClick: () => void;
}
const Component = ({handlePress}: Props) => (
<View>
<Button title="Click Me" onPress={handlePress} />
</View>
);
export default compose<Props>(
withNavigation(),
withCallback<Props>('handlePress', ({navigation}) => () => {
navigation.goBack();
}),
)(Component);
import React from 'react';
import {compose} from '@truefit/bach';
import {withNavigation} from '@truefit/bach-react-navigation';
import {View, Button} from 'react-native';
const Component = ({handlePress}) => (
<View>
<Button title="Click Me" onPress={handlePress} />
</View>
);
export default compose<Props>(
withNavigation(),
withCallback<Props>('handlePress', ({navigation}) => () => {
navigation.goBack();
}),
)(Component);
React Navigation Hook useNavigation
Gives access to the navigation state of the navigator which contains the screen.
Example
import React from 'react';
import {compose} from '@truefit/bach';
import {withNavigationState} from '@truefit/bach-react-navigation';
import {View, Text} from 'react-native';
type Props = {
routeNames: string;
}
const Component = ({routeNames}: Props) => (
<View>
<Text>{routeNames}</Text>
</View>
);
export default compose<Props>(
withNavigationState('routeNames', state => state.routeNames.join(',')),
)(Component);
import React from 'react';
import {compose} from '@truefit/bach';
import {withNavigationState} from '@truefit/bach-react-navigation';
import {View, Text} from 'react-native';
const Component = ({routeNames}) => (
<View>
<Text>{routeNames}</Text>
</View>
);
export default compose(
withNavigationState('routeNames', state => state.routeNames.join(',')),
)(Component);
React Navigation Hook useNavigationState
Gives access to route object. It's useful when you cannot pass the route prop into the component directly, or don't want to pass it in case of a deeply nested child.
Example
import React from 'react';
import {compose} from '@truefit/bach';
import {withRoute} from '@truefit/bach-react-navigation';
import {RouteProp} from '@react-navigation/native';
import {NavigationProp} from '<your navigation prop>';
import {View, Text} from 'react-native';
type Props = {
route: RouteProp<NavigationProp, 'RouteKey'>;
}
const Component = ({route}: Props) => (
<View>
<Text>{route.key}</Text>
</View>
);
export default compose<Props>(
withRoute(),
)(Component);
import React from 'react';
import {compose} from '@truefit/bach';
import {withRoute} from '@truefit/bach-react-navigation';
import {View, Text} from 'react-native';
const Component = ({route}) => (
<View>
<Text>{route.key}</Text>
</View>
);
export default compose<Props>(
withRoute(),
)(Component);
React Navigation Hook useRoute
The expected native behavior of scrollable components is to respond to events from navigation that will scroll to top when tapping on the active tab as you would expect from native tab bars.
React Navigation Hook useScrollToTop