2

我需要用 ThemeProvider 标记将我的应用程序主组件括起来,但我不知道在哪里将其括起来,因为我使用的是 react-navigation 中的 appContainer,看起来我需要将 appContainer 括起来。

我正在一个没有博览会的本地反应项目中工作。我正在导入 react-native-elements 和 react-navigation。我需要附上已经被 appContainer 包装的 app.js

应用程序.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',
  }

));

index.js

/**
 * @format
 */

import {AppRegistry} from 'react-native';
import App from './src/App';
import {name as appName} from './app.json';

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

抱歉,我还没有任何输出,谢谢您的时间。

4

2 回答 2

2

createAppContainer返回React Component。因此,您可以使用 any 包装它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);
于 2019-07-02T13:19:15.573 回答
0

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

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

我使用<ThemeProvider/>from react-native-elements 包装 createAppContainer 返回的组件。主题道具是 lightTheme 或 darkTheme 对象,具体取决于 lightThemeState。

我发送了一个功能,屏幕组件可以使用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>
  );
};

我还在这里更详细地提出了一个问题.. 用反应原生元素改变主题不起作用?

将不胜感激任何帮助.. 谢谢!

于 2020-04-20T00:18:50.003 回答