每个堆栈的默认导航样式

时间:2019-01-10 18:54:09

标签: reactjs react-native react-navigation react-navigation-stack

我有一个默认的导航样式,我想将其合并到所有堆栈中,所以我有这个对象。

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>)
}

但是,如果我执行此操作,navigation.navigate将因为未定义而无法使用。

无论如何,我可以通过执行以下操作将此对象应用于每个堆栈:

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) } });

1 个答案:

答案 0 :(得分:1)

您可以尝试通过导航道具

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>)
}}

然后是

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