Search code examples
reactjsreact-nativereact-navigationreact-navigation-stack

Default Navigation Style for every stack


I have a default navigation style that I want to incorporate to all of my stacks so I have this object.

const navigationDefaultStyle = {
  headerStyle: { backgroundColor:"#fff", height:60},
  headerTitle: (<View style={{marginTop:5, flex: 1, flexDirection: 'row', justifyContent: 'center' }}><Image style={{ width:140, height: 36, marginTop: 0, marginRight: 5}} source={require('./app/images/logo.png')}/></View>),
  headerRight: (<TouchableOpacity activeOpacity={0.8} onPress={ async () => {

      navigation.navigate('Settings', {transitionStyle:'default'})

    } } style={{ marginRight:15, marginTop:5 }}>
      <Icon name="cog" type="light" color="#29317C" size={26} />
    </TouchableOpacity>)
}

However if I do this, the navigation.navigate doesn't work because it is not defined.

Is there anyway I can apply this object to every stack by doing this:

const Login = createStackNavigator({ SignIn: { screen: SignInScreen, navigationOptions: ({ navigation }) => (navigationDefaultStyle) } });
const Sync = createStackNavigator({ Sync: { screen: SyncScreen, navigationOptions: ({ navigation }) => (navigationDefaultStyle) } });
const Identification = createStackNavigator({ Identification: { screen: IDScreen, navigationOptions: ({ navigation }) => (navigationDefaultStyle) } });

Solution

  • You can try passing the navigation prop

    const navigationDefaultStyle = (navigation)=>{
      return{
       headerStyle: { backgroundColor:"#fff", height:60},
       headerTitle: (<View style={{marginTop:5, flex: 1, flexDirection: 'row', justifyContent: 'center' }}><Image style={{ width:140, height: 36, marginTop: 0, marginRight: 5}} source={require('./app/images/logo.png')}/></View>),
       headerRight: (<TouchableOpacity activeOpacity={0.8} onPress={ async () => {
    
          navigation.navigate('Settings', {transitionStyle:'default'})
    
        } } style={{ marginRight:15, marginTop:5 }}>
          <Icon name="cog" type="light" color="#29317C" size={26} />
        </TouchableOpacity>)
    }}
    

    and then just

    const Login = createStackNavigator({ SignIn: { screen: SignInScreen, navigationOptions: ({ navigation }) => (navigationDefaultStyle(navigation)) } });