Extend an Screen options in react-native

Viewed 3824

i have several screens in stack navigator like below

   <Stack.Navigator screenOptions={TransitionScreenOptions}>
      <Stack.Screen
        options={stackoptions}
        name="videoScreen"
        component={videoScreen}
      />
      <Stack.Screen
        options={stackoptions}
        name="registerScreen"
        component={SignUpScreen}
      />
</Stack.Navigator>

for all the screens i set options like below

const stackoptions = ({ navigation }) => ({
  headerBackTitleVisible: false,
  headerStyle: {
    backgroundColor: "#000",
  },
  headerTintColor: "#fff",
  headerRight: () => {
    return (
      <Pressable
        style={{ paddingRight: 8 }}
        onPress={() => navigation.toggleDrawer()}
      >
        <AntDesign name="menu-fold" size={30} color="white" />
      </Pressable>
    );
  },
});

Now , how can i override or add certain options to one of my screen , for example something like below , I want to modify only headerTintColor , whereas remaining stackoptions should be used except headerTintColor.

  <Stack.Screen
             options={...stackoptions, headerTitle: "" }
            name="registerScreen"
            component={SignUpScreen}
          />
1 Answers

for all the screens i set options like below

This is unnecessary. You can specify common options in screenOptions and they'll be applied to all screens, it's not needed to repeat them for every screen. You can use spread operator to merge your TransitionScreenOptions with the others:

<Stack.Navigator screenOptions={({ navigation }) => ({
  ...TransitionScreenOptions,
  headerBackTitleVisible: false,
  headerStyle: {
    backgroundColor: "#000",
  },
  headerTintColor: "#fff",
  headerRight: () => {
    return (
      <Pressable
        style={{ paddingRight: 8 }}
        onPress={() => navigation.toggleDrawer()}
      >
        <AntDesign name="menu-fold" size={30} color="white" />
      </Pressable>
    );
  },
})}>

You can just do this:

options={{ headerTitle: "" }}

This will override the same option if it was specified in screenOptions

If you need to use the current structure, call the stakOptions function and pass the arguments before spreading it:

options={args => ({
  ...stackoptions(args),
  headerTitle: ""
})}
Related