如何使用react-native-elements的主题提供者标签包装appContainer?

时间:2019-07-01 17:41:27

标签: react-native user-interface react-navigation react-native-elements

我需要用ThemeProvider标签将应用程序的主要组件封装起来,但是我不知道在何处封装它,因为我使用的是来自react-navigation的appContainer,因此好像需要封装appContainer。

我在一个没有展览的本地项目中工作。我正在导入react-native-elements和react-navigation。我需要封装已经由appContainer包装的app.js

App.js

import socket


def ConnectionTest():
    domain = "www.something.com/uptimecheck"

    try:
        socket.create_connection((domain, 443))
        return True
    except OSError:
        pass
    return False

index.js

import { createSwitchNavigator, createStackNavigator, createAppContainer } from 'react-navigation';
import PantallaInicio from './components/home/PantallaInicio';
import PantallaLogin from './components/auth/PantallaLogin';
import PantallaRegistro from './components/auth/PantallaRegistro';
import PantallaCargando from './components/auth/PantallaCargando';

const AppStack = createStackNavigator({ Home: PantallaInicio, });
const AuthStack = createStackNavigator({ Login: PantallaLogin, Registro: PantallaRegistro });

export default createAppContainer(createSwitchNavigator(
  {
    AuthLoading: PantallaCargando,
    App: AppStack,
    Auth: AuthStack,
  },
  {
    initialRouteName: 'AuthLoading',
  }

));

对不起,我还没有任何输出,谢谢您的时间。

2 个答案:

答案 0 :(得分:0)

createAppContainer返回React Component。因此,您可以使用任何provider来包装它。

import React from 'react';
import {AppRegistry} from 'react-native';
import App from './src/App';
import { ThemeProvider } from 'react-native-elements';
import theme from './your-theme';
import {name as appName} from './app.json';

const ProvidedApp = () => {
  return <ThemeProvider theme={theme} ><App /></ThemeProvider>
}

AppRegistry.registerComponent(appName, () => ProvidedApp);

答案 1 :(得分:0)

我有点想借这个线程来解决Jeff Gu Kang所说的问题。

Jeff Gu Kang:“您可以使用任何ThemeProvider”

我正在使用来自react-native-elements的<ThemeProvider/>来包装createAppContainer返回的组件。主题道具取决于lightThemeState是lightTheme还是darkTheme对象。

我使用screenProps传递了一个函数,屏幕组件可以使用该函数将主题从浅色更改为深色。但是屏幕不会更新...如果将lightThemeNav的状态更改为false,一切都会更改并且可以正常工作,但是由于某些原因,当我切换按钮时,它不会更新主题。 (按钮可以正确更改状态)

代码如下:


const TabNavigator = createMaterialBottomTabNavigator(
  {
    FindDestinationScreen: {
      screen: FindDestinationScreen,
      navigationOptions: {
        title: "Search",
        tabBarIcon: ({ tintColor }) => (
          <SafeAreaView>
            <Icon
              style={[{ color: tintColor }]}
              size={25}
              name={"ios-search"}
            />
          </SafeAreaView>
        ),
      },
    },
  },
  {
    barStyleDark: {
      backgroundColor: "#212121",
      shadowColor: "#000",
      shadowOffset: { width: 3, height: 2 },
      shadowOpacity: 0.8,
      shadowRadius: 2,
      elevation: 1,
    },
    barStyleLight: {
      backgroundColor: "#3c5e82",
    },
    shifting: false,
    labeled: true,
    initialRouteName: "FindDestinationScreen",
    activeColor: "#E4DC93",
    inactiveColor: "#fff",
    barStyle: { backgroundColor: "transparent", height: 80, paddingTop: 10 },
  }
);

const AllRoutes = createSwitchNavigator(
  {
    PersonalSettings: {
      title: "Personal Settings",
      screen: PersonalSettings,
      header: ({ goBack }) => ({
        left: (
          <Icon
            name={"chevron-left"}
            onPress={() => {
              goBack();
            }}
          />
        ),
      }),
    },
    Tabs: {
      screen: TabNavigator,
    },
  },
  {
    initialRouteName: "Tabs",
  }
);

const AppContainer = createAppContainer(AllRoutes);

export default App = () => {
  const [lightThemeState, setLightThemeState] = useState(true);

  return (
      <ThemeProvider theme={lightThemeState ? lightTheme : darkTheme}>
        <AppContainer
          theme={lightThemeState ? "light" : "dark"}
          screenProps={{
            setLightThemeState: setLightThemeState,
          }}
        />
      </ThemeProvider>
  );
};

我也在这里更详细地提出了一个问题。 changing theme with react native elements not working?

将非常感谢您的帮助。