在 Ionic React 中隐藏登录屏幕上的选项卡

kka*_*roo 4 reactjs ionic-framework react-router ionic-react

我创建了一个带有来自 cli 的 tabs starter 模板的 ionic react 应用程序,并将登录功能添加到现有结构中。

我做了什么 :

应用程序.tsx

  const App: React.FC = () => (
  <IonApp>
    <IonReactRouter>
      <IonTabs>
        <IonRouterOutlet>
          <Route path="/profile" component={Profile} exact={true} />
          <Route path="/history" component={History} />
          <Route path="/login" component={Login} exact={true} />
          <Route path="/" component={Login} exact={true} />
        </IonRouterOutlet>
        <IonTabBar slot="bottom">
          <IonTabButton tab="profile" href="/profile">
            <IonIcon icon={personCircleOutline} />
            <IonLabel>Profile</IonLabel>
          </IonTabButton>
          <IonTabButton tab="history" href="/history">
            <IonIcon icon={documentTextOutline} />
            <IonLabel>History</IonLabel>
          </IonTabButton>
        </IonTabBar>
      </IonTabs>
    </IonReactRouter>
  </IonApp>
);
Run Code Online (Sandbox Code Playgroud)

问题是我在登录屏幕上看到了所有选项卡。我希望选项卡仅在用户登录后显示。

工作演示在这里

htm*_*tmn 9

您需要将您的私人标签分成另一个组件:

mainTabs.tsx

const MainTabs: React.FC = () => {
  return (
    <IonTabs>
      <IonRouterOutlet>
        <Redirect exact path="/" to="/profile" />
        <Route path="/profile" render={() => <Profile />} exact={true} />
        <Route path="history" render={() => <History />} exact={true} />
      </IonRouterOutlet>
      <IonTabBar slot="bottom">
        <IonTabButton tab="profile" href="/profile">
          <IonIcon icon={personCircleOutline} />
          <IonLabel>Profile</IonLabel>
        </IonTabButton>
        <IonTabButton tab="history" href="/history">
          <IonIcon icon={documentTextOutline} />
          <IonLabel>History</IonLabel>
        </IonTabButton>
      </IonTabBar>
    </IonTabs>
  );
};

export default MainTabs;
Run Code Online (Sandbox Code Playgroud)

之后,您可以在App.tsx中创建一个路由,如果用户登录,您可以在其中有条件地呈现私有选项卡。否则,将呈现登录页面:

应用程序.tsx

return (
  <IonApp>
    <IonReactRouter>
      <Route path="/login" component={Login} exact={true} />
      <Route path="/" component={isLoggedIn ? MainTabs : Login} />
    </IonReactRouter>
  </IonApp>
);
Run Code Online (Sandbox Code Playgroud)

在这一点上,您想要进行某种状态管理设置(Redux、通过 React 钩子进行自定义状态管理等),因为您需要isLoggedIn在 App 组件中拥有您的状态,但需要从 Login 组件中修改它(这实际上很混乱)仅快速使用道具)。

所以作为一个简单的例子(你绝对可以做得更好),我创建了一个简单的user上下文,我在其中注入了更新isLoggedIn状态的函数:

应用程序.tsx

interface IUserManager {
  setIsLoggedIn: Function;
}

const user: IUserManager = {
  setIsLoggedIn: () => {}
};

export const UserContext = React.createContext<IUserManager>(user);

const IonicApp: React.FC = () => {
  const [isLoggedIn, setIsLoggedIn] = useState(false);
  const user = useContext(UserContext);

  user.setIsLoggedIn = setIsLoggedIn;

  return (
    <IonApp>
      <IonReactRouter>
        <Route path="/login" component={Login} exact={true} />
        <Route path="/" component={isLoggedIn ? MainTabs : Login} />
      </IonReactRouter>
    </IonApp>
  );
};

const App: React.FC = () => {
  return (
    <UserContext.Provider value={user}>
      <IonicApp />
    </UserContext.Provider>
  );
};
Run Code Online (Sandbox Code Playgroud)

我还需要创建一个顶级App组件,以便我可以提供上下文并立即在IonApp组件内部使用它。之后,我可以简单地使用组件user内的上下文Login并在登录时更新状态:

const user = useContext(UserContext);

const loginClick = () => {
  if (userName === "a" && password === "a") {
    user.setIsLoggedIn(true);
  } 
};
Run Code Online (Sandbox Code Playgroud)

这是你的分叉演示