简体   繁体   English

如何在 React Navigation Native 中正确使用 if 语句?

[英]How to properly use if statements in React Navigation Native?

I'm building an application in which user could see information in different languages.我正在构建一个应用程序,用户可以在其中查看不同语言的信息。 User could also choose a language in which information would be displayed.用户还可以选择显示信息的语言。 However, I run into a problem when I try to set a language for my navigation bar.但是,当我尝试为导航栏设置语言时遇到问题。 I'm using React Navigation Native to create navigation bar and useContext hook to get chosen language from another component.我正在使用 React Navigation Native 创建导航栏并使用 useContext 挂钩从另一个组件获取所选语言。 If I use if statement inside Navigator, I get an error:如果我在 Navigator 中使用 if 语句,我会得到一个错误:

Error: Couldn't find any screens for the navigator.错误:找不到导航器的任何屏幕。 Have you defined any screens as its children?您是否将任何屏幕定义为其子屏幕?

As per my understanding application on mount doesn't get variable "locale" and it doesn't have anything to display.根据我的理解,mount 上的应用程序没有变量“locale”,也没有任何可显示的内容。 How could I avoid that (should I use async function somewhere?)?我怎样才能避免这种情况(我应该在某处使用 async function 吗?)?

This is my code:这是我的代码:

App.js code App.js代码

function App() {
  const [locale, setLocale] = useState("en");

  return (
    <LanguageContext.Provider value={{ locale, setLocale }}>
      <MainContainer />
    </LanguageContext.Provider>
  );
}

LanguageContext.js LanguageContext.js

import { createContext } from "react";

export const LanguageContext = createContext("");

MainContainer.js主容器.js

const profileName = "Profile";
const detailsName = "Details";
const settingsName = "Settings";
const profileNameFR = "P";
const detailsNameFR = "D";
const settingsNameFR = "S";

const Tab = createBottomTabNavigator();

export default function MainContainer() {
  const { locale } = useContext(LanguageContext);
  
  return (
    <NavigationContainer>
      <Tab.Navigator
        screenOptions={({ route }) => ({
          tabBarIcon: ({ focused, color, size }) => {
            let iconName;

            let rn = route.name;

            if (rn === profileName || rn === profileNameFR) {
              iconName = focused ? "person" : "person-outline";
            } else if (rn === detailsName || rn === detailsNameFR) {
              iconName = focused ? "list" : "list-outline";
            } else if (rn === settingsName || settingsNameFR) {
              iconName = focused ? "settings" : "settings-outline";
            }

            return <Ionicons name={iconName} size={size} color={color} />;
          },
          tabBarActiveTintColor: "tomato",
          inactiveTintColor: "grey",
          tabBarStyle: { padding: 10, height: 60 },
          tabBarLabelStyle: { paddingBottom: 10, fontSize: 10 },
          style: { padding: 10 },
        })}
      >
        {locale === "en" && (
          <>
            <Tab.Screen name={profileName} component={ProfileScreen} />
            <Tab.Screen name={detailsName} component={DetailsScreen} />
            <Tab.Screen name={settingsName} component={SettingsScreen} />
          </>
        )}
        {locale === "fr" && (
          <>
            <Tab.Screen name={profileNameFR} component={ProfileScreen} />
            <Tab.Screen name={detailsNameFR} component={DetailsScreen} />
            <Tab.Screen name={settingsNameFR} component={SettingsScreen} />
          </>
        )}
      </Tab.Navigator>
    </NavigationContainer>
  );
}

I totally forgot about useState that I was using to set the language in main component.我完全忘记了我用来在主要组件中设置语言的 useState。 In the beginning it was一开始是

const [locale, setLocale] = useState("");

Which meant that when app was rendering locale variable's value it was empty string.这意味着当应用程序呈现语言环境变量的值时,它是空字符串。 All I needed to do was to set the state to default application's language, which in my case was "en", and then the application will work.我需要做的就是将 state 设置为默认应用程序的语言,在我的例子中是“en”,然后应用程序就会运行。

Also I leave my main component just in case somebody wonders how I use useContext in this scenario:此外,我保留了主要组件,以防万一有人想知道我在这种情况下如何使用 useContext:

App.js code App.js代码

function App() {
  const [locale, setLocale] = useState("en");

  return (
    <LanguageContext.Provider value={{ locale, setLocale }}>
      <MainContainer />
    </LanguageContext.Provider>
  );
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM