首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使用react-native-elements中的主题提供者标记包装appContainer?

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

Stack Overflow用户
提问于 2019-07-02 01:41:27
回答 2查看 975关注 0票数 2

我需要用ThemeProvider标签封装我的应用程序主组件,但我不知道在哪里封装它,因为我使用的是来自react-navigation的appContainer,看起来我需要改为封装appContainer。

我在一个没有expo的react-native项目中工作。我正在导入反应原生元素和反应导航。我需要封装已经被appContainer包装的app.js

App.js

代码语言:javascript
复制
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

代码语言:javascript
复制
/**
 * @format
 */

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

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

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

EN

回答 2

Stack Overflow用户

发布于 2019-07-02 21:19:16

createAppContainer返回React Component。因此,您可以使用任何provider对其进行包装。

代码语言:javascript
复制
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);
票数 2
EN

Stack Overflow用户

发布于 2020-04-20 08:18:50

我借用这个帖子是为了解决关于Jeff Gu Kang所说的一个问题..

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

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

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

代码如下:

代码语言:javascript
复制
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?

会非常感谢任何形式的帮助..谢谢!

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/56840117

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档