Skip to content

Instantly share code, notes, and snippets.

@mortocks
Last active November 26, 2022 14:32
Show Gist options
  • Save mortocks/57ad889566f888e6906a05d6eaea4e69 to your computer and use it in GitHub Desktop.
Save mortocks/57ad889566f888e6906a05d6eaea4e69 to your computer and use it in GitHub Desktop.
Override the back button action in React Navigations stack header. Useful if you want to pass parameters back to the previous screen #react-native #react-navigation
import { useLayoutEffect } from 'react';
import { HeaderBackButton } from '@react-navigation/stack';
/**
* Override the back button action in React Navigations stack header
* @param { Effect } The action to be triggered
*/
const useBackButtonEffect = effect => {
useLayoutEffect(() => {
navigation.setOptions({
headerLeft: (props) => (
<HeaderBackButton {...props} onPress={effect} />
),
})
}, [navigation] );
}
export default useBackButtonEffect;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment