Repository navigation
Is it okay to set presentation in a useEffect with setOptions, or would it hurt performance?
#4067
|
Consider a screen like this: const SheetScreen = ({ navigation }) => {
useEffect(() => {
navigation.setOptions({
title: 'Test',
presentation: 'formSheet',
sheetAllowedDetents: [0.5],
});
}, [navigation]);
return (
<View style={{ padding: 24 }}>
<Text>Form sheet</Text>
</View>
);
};This pattern is good for DX because it keeps implementation details of the screen inside that screen's component. This code also works on both Android and iOS — the screen is presented as a bottom sheet from the start. However, considering that I know that Thanks! |
Replies: 2 comments 1 reply
|
Hey! I'm surprised that it's the first time somebody asks and that we haven't documented that in the native stack docs in react-navigation, however setting presentation in useEffect does not make sense and is not supported. The intended presentation for the screen MUST be known at first-render time. Imagine that after first render, on native side I receive a new screen with Hence, it is not supported and likely won't be. |
|
For Those options are structural native-stack options. They are ideally known before the native screen is created/presented. A late The safer pattern is to keep the option on the <Stack.Screen
name="Sheet"
component={SheetScreen}
options={{
title: 'Test',
presentation: 'formSheet',
sheetAllowedDetents: [0.5],
}}
/>If you want colocation, export the options from the screen module and import them into the navigator: export const sheetScreenOptions = {
title: 'Test',
presentation: 'formSheet',
sheetAllowedDetents: [0.5],
} satisfies NativeStackNavigationOptions
|
Hey!
I'm surprised that it's the first time somebody asks and that we haven't documented that in the native stack docs in react-navigation, however setting presentation in useEffect does not make sense and is not supported. The intended presentation for the screen MUST be known at first-render time.
Imagine that after first render, on native side I receive a new screen with
presentation: 'push', I push it onto the stack with e.g. UINavigationController, and then, when transition has already been triggered I receive result of another render informing me (native implementation) that you wish to present that screen asformSheet. How should that even look like?Hence, it is not supported and …