如何在反应导航中使用用户指定的主题?

零基_IX

我一直在尝试为我的 react-navigation 导航器设置主题,这很容易使用硬编码颜色,即Colours.background, Colours.accent.

当用户通过身份验证时,我会提供他们的基本详细信息和主题对象。这使用 redux ( state.auth.user.theme)存储在我的应用程序状态中

我有一个 AppNavigation,我的导航器是在其中创建的。

应用导航.js

import React from 'react'
import { Text, Animated, Easing, Image, View } from 'react-native'
import { StackNavigator, DrawerNavigator, addNavigationHelpers } from 'react-navigation'
import Icon from 'react-native-vector-icons/EvilIcons'
import { connect } from 'react-redux'

import DrawerContainer from '../Containers/DrawerContainer'
import ScreenTwoScreen from '../Containers/ScreenTwoScreen'
import ScreenOneScreen from '../Containers/ScreenOneScreen'
import DashboardScreen from '../Containers/DashboardScreen'
import LoginScreen from '../Containers/LoginScreen'

import styles from './Styles/NavigationStyles'
import ThemeProvider from '../Themes/ThemeProvider';
import Images from '../Themes/Images';

const Colors = ThemeProvider()

const DrawerStack = DrawerNavigator({
  screenOneScreen: { screen: ScreenOneScreen },
  dashboardScreen: { screen: DashboardScreen },
}, {
    gesturesEnabled: false,
    contentComponent: DrawerContainer
  })

const drawerButton = (navigation) =>
    <Text
      style={{ padding: 5, color: 'white' }}
      onPress={() => {
        // Coming soon: navigation.navigate('DrawerToggle')
        // https://github.com/react-community/react-navigation/pull/2492
        if (navigation.state.index === 0) {
          navigation.navigate('DrawerOpen')
        } else {
          navigation.navigate('DrawerClose')
        }
      }
      }><Icon name="navicon" color={Colors.headerText} size={24} /></Text>


// login stack
const LoginStack = StackNavigator({
  loginScreen: { screen: LoginScreen },
}, {
    headerMode: 'none',
    navigationOptions: {
    }
  })

const logoStyle = {
  height: 45,
  width: 45,
  marginLeft: 10,
  resizeMode: 'contain'
}
const DrawerNavigation = StackNavigator({
  DrawerStack: { screen: DrawerStack }
}, {
    headerMode: 'float',
    navigationOptions: ({ navigation }) => ({
      headerStyle: { backgroundColor: Colors.brand },
      title: 'Welcome!',
      headerTintColor: Colors.headerText,
      gesturesEnabled: false,
      headerRight: drawerButton(navigation),
      headerLeft: <Image source={Images.clubLogo} style={logoStyle} />
    })
  })


// Manifest of possible screens
const PrimaryNav = StackNavigator({
  loginStack: { screen: LoginStack },
  drawerStack: { screen: DrawerNavigation }
}, {
    // Default config for all screens
    headerMode: 'none',
    initialRouteName: 'loginStack',
    navigationOptions: {
      headerStyle: styles.header
    }
  })

export default PrimaryNav

这在 ReduxNavigation.js 中使用:

ReduxNavigation.js

import React from 'react'
import * as ReactNavigation from 'react-navigation';
import { connect } from 'react-redux'
import AppNavigation from './AppNavigation'
import Colors from '../Themes/Colors'
import ThemeProvider from '../Themes/ThemeProvider'
// here is our redux-aware our smart component
class ReduxNavigation extends React.Component {
  static navigationOptions = ({ navigation, screenProps }) => {
    console.log(navigation)
    return {
      headerStyle: {
        backgroundColor: this.props.user ? this.props.user.club.brand : Colors.brand
      }
    }
  }
  render() {
    const { dispatch, nav, user } = this.props
    const navigation = ReactNavigation.addNavigationHelpers({
      dispatch,
      state: nav
    })


    // if (user) {
    //   theme = ThemeProvider(user.club.theme)
    // }
    return <AppNavigation navigation={navigation} />
  }

}

const mapStateToProps = state => ({ nav: state.nav, user: state.auth.user })
export default connect(mapStateToProps)(ReduxNavigation)

主题提供者.js

没有什么花哨:

import Colors from './Colors';

const ThemeProvider = (other ={}) => {
    return Object.assign({}, Colors, other)
}

export default ThemeProvider;

我尝试过的事情

  • 创建了一个将用户主题合并为颜色的主题提供程序方法,它有效但不正确。
  • 将 AppNavigation 的核心移动到 ReduxNavigation 的渲染方法中,但这使得应用程序无响应,这是可以理解的。
  • 试图找出一种将主题作为道具传递给 PrimaryNav 的方法,react-navigation 不喜欢额外的道具。

想法和想法?此外,如果有任何用途,应用程序的核心是使用InfiniteRed/ignite生成的。

零基_IX

想出一个办法,screenProps在ReduxNavigation中使用

class ReduxNavigation extends React.Component {

  render() {
    const { dispatch, nav, user } = this.props
    const navigation = ReactNavigation.addNavigationHelpers({
      dispatch,
      state: nav
    })
    return <AppNavigation navigation={navigation} screenProps={{ user }} />
  }

}

然后在导航器上的 AppNavigation 中,我需要自定义主题:

DrawerNavigation = StackNavigator({
  DrawerStack: { screen: DrawerStack }
}, {
    headerMode: 'float',
    navigationOptions: ({ navigation, screenProps }) => {
      let headerText = Colors.headerText;
      let brand = Colors.brand;
      let accent = Colors.accent;
      if (screenProps.user) {
        let theme = screenProps.user.club.theme;
        brand = theme.brand;
        headerText = theme.headerText;
        accent = theme.accent;
      }
      return ({
        headerStyle: { backgroundColor: brand },
        title: 'Welcome!',
        headerTintColor: headerText,
        gesturesEnabled: false,
        headerRight: drawerButton(navigation, headerText),
        headerLeft: <Image source={Images.clubLogo} style={logoStyle} />
      })
    }
  })

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如何在函数中使用用户定义类型?

如何在laravel中使用用户名登录

如何在胡子PHP中使用用户定义的函数

如何在条件(MATLAB)中使用用户输入

如何在集群中使用用户输入(Node JavaScript)

如何在查询中使用用户@param

如何在Mysql If子句中使用用户变量?

如何在PHP / cURL中使用用户IP?

如何在海龟模块中使用用户输入语句?

反应导航-如何在标题NavigationOptions中使用this.state?

如何在其他类中使用反应导航?

如何在alert onpress中使用反应导航?

如何使用用户指定的种子在elm 0.17中使用随机生成器?

在Javascript中使用用户指定的参数作为键

如何在PowerShell控制台中使用用户名和密码使用wget

如果我需要使用用户输入的数字,如何在 switch 中使用枚举?

如何在反应组件外使用反应导航进行导航?

如何在IF表达式中使用用户定义的变量?

如何在打字稿中使用用户制作的条件进行评估

Auth0:如何在规则中使用用户配置文件?

很难弄清楚如何在 Java 的 for 循环中使用用户输入

如何在Perl中使用用户输入来运行本地程序

如何在Linux命令中使用用户定义的变量名?

如何在Winform的构造方法中使用用户定义的属性?

如何在终端的一行中使用用户名和密码运行openconnect?

如何在变量中使用用户从文本框中输入的值(Powershell)

如何在Codeigniter3.0.6中使用用户代理?

如何在Axios中使用用户名和密码访问数据库

如何在闪亮的应用程序中使用用户编写的功能?