简体   繁体   English

当我在 react 中选择登录或注册时如何隐藏导航栏?

[英]How do I hide navbar when i select login or signup in react?

I am trying to hide the Menu component when I select log in my app.当我选择登录我的应用程序时,我试图隐藏菜单组件。 I am working with react hooks and I have no idea how to do it.我正在使用react hooks ,但我不知道该怎么做。

My main looks like this :我的主要看起来像这样:

 <div>
        <Menu/>
        <Router>
            {
                domainList === "error" ?
                    (
                        <ErrorMessage
                            message="Error"
                        />
                    )
                    :
                    Boolean(domainList) === true ?
                        (
                            <Main
                                endpoint={endpoint}
                                callbackReFetchDomains={reFetchDomains}
                                domainList={domainList}
                                hasDomainListError={hasDomainListError}
                                appendDomainList={appendDomainList}
                                changeDomainList={changeDomainList}
                            />
                        )
                        :
                        (
                            <LoadingSpinner/>
                        )
            }
        </Router>
    </div>

My main looks like this :我的主要看起来像这样:

<>
        <div>
            {/*Switch will only render the first matched <Route/> child.*/}
            <Menu/>
            <Switch>
                <Route path="/topics">
                    <ExampleComponentStructure/>
                </Route>
                <Route path="/login">
                    <Login/>
                </Route>
                <Route path="/domains">
                    <DomainList
                        endpoint={props.endpoint}
                        callbackReFetchDomains={props.callbackReFetchDomains}
                        domainList={props.domainList}
                        hasDomainListError={props.hasDomainListError}
                        appendDomainList={props.appendDomainList}
                        changeDomainList={props.changeDomainList}
                    />
                </Route>
                <Route path="/signup">
                    <Signup/>
                </Route>
                <Route path="/users">
                    <UserMaintainList
                        endpoint={props.endpoint}
                    />
                </Route>
                <Route path="/">
                    <StickerList
                        endpoint={props.endpoint}
                        callbackReFetchDomains={props.callbackReFetchDomains}
                        domainList={props.domainList}
                        hasDomainListError={props.hasDomainListError}
                        changeDomainList={props.changeDomainList}
                    />
                </Route>

            </Switch>
        </div>
    </>

I know that the code is not clean.我知道代码不干净。 I am just starting with react and need some help doing this login screen.我刚刚开始使用 react,需要一些帮助来完成这个登录屏幕。 Thank you.谢谢你。

If i understood correct, you want to hide component Menu (is it navbar?).如果我理解正确,您想隐藏组件菜单(是导航栏吗?)。 First you can check url you are in, by creating some flag, for example:首先,您可以通过创建一些标志来检查您所在的网址,例如:

const isLogin = props.match.path === "/login"

And then just render component if it is false然后只是渲染组件,如果它是假的

{!isLogin && <Menu/>}

here is how i done这是我做的

       <PublicRoute exact path="/welcome" component={WelcomePageView} />
       <PublicRoute exact path="/signin" component={SignInView} />
        <PublicRoute
          exact
          path="/forgotPassword"
          component={ResetPasswordView}
        />
        <PublicRoute
          exact
          path="/resetPassword"
          component={SetNewPasswordView}
        />
        <RouteWithLayout
          component={UserDashboardView}
          exact
          layout={MainLayout}
          noAccess={true}
          path="/dashboard"
        />
        <RouteWithLayout
          component={ProfileView}
          exact
          layout={MainLayout}
          path="/Profile"
        />

PublicRoute.jsx公共路由.jsx

import React, { useEffect } from "react"
import { Route, Redirect } from "react-router"
import { connect } from "react-redux"
import { isUserAuthenticated, getAuthErrors, getAccessToken, getUserID } from "../selectors"
import { validateToken, logout } from "../actions/auth"
import { getPersistStatus } from "../selectors/persist"


const PublicRoute = ({
    component: Component,
    isAuthenticated,
    authErrors,
    access_token,
    uid,
    persistStatus,
    logoutUser,
    ...rest
}) => {

    useEffect(() => {
      if (persistStatus && !isAuthenticated) {
            logoutUser()
      }
    }, [persistStatus, isAuthenticated, logoutUser])

    return (
        <Route {...rest} render={props => (
            !isAuthenticated ? (
                <Component {...props} />
            ) : (
                    <Redirect to={{
                        pathname: "/dashboard",
                        state: { from: props.location }
                    }} />
                )
        )} />
    )
}

const mapStateToProps = (state) => ({
    isAuthenticated: isUserAuthenticated(state),
    authErrors: getAuthErrors(state),
    access_token: getAccessToken(state),
    uid: getUserID(state),
    persistStatus: getPersistStatus(state)
})

const mapDispatchToProps = (dispatch) => ({
    logoutUser: () => {
        dispatch(logout())
    }
})

export default connect(mapStateToProps, mapDispatchToProps)(PublicRoute)

RouteWithLayout.jsx RouteWithLayout.jsx

import React from "react"
import { Route } from "react-router-dom"
import PropTypes from "prop-types"

import { useAuthorization } from "./Permissions"

const RouteWithLayout = (props) => {
  const { layout: Layout, component: Component, path, noAccess, ...rest } = props

  const userAccess = useAuthorization(path)

  return (
    <Route
      {...{ path, ...rest }}
      render={(matchProps) => (
        <Layout>
          {noAccess || userAccess.includes("R") ? (
            <Component {...matchProps} />
          ) : (
            <p>You do not have sufficient permissions to view this page</p>
          )}
        </Layout>
      )}
    />
  )
}

RouteWithLayout.propTypes = {
  component: PropTypes.any.isRequired,
  layout: PropTypes.any.isRequired,
  path: PropTypes.string,
}

export default RouteWithLayout

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

相关问题 如何在登录和注册中隐藏导航栏? - How to hide NavBar in login & signup? 尝试打印页面时如何隐藏导航栏 - How do I hide the navbar when trying to print the page 当用户点击其他地方时,如何隐藏导航栏的下拉菜单? - How do I hide the dropdown of a navbar when a user clicks elsewhere? 如何在Meteor中创建独立的注册/登录页面? - How do I create a standalone signup/ login page in Meteor? 如何在反应路由器的登录页面中隐藏导航栏 - How to hide navbar in login page in react router 在带有reactjs的手机中使用时,如何隐藏网站的NavBar? - How do I hide NavBar of my website when using in mobile with reactjs? 在React中滚动时,如何使导航栏突出显示? - How do I make my navbar highlight as I scroll in React? React - 用户登录后如何在导航栏中隐藏注册/登录? - React - How to hide register/login in navbar once a user has logged in? 在 React 登录组件中,Firebase 登录注册正在运行,但是当我尝试发送重置密码链接时,则未发送到电子邮件 - In React Login component Firebase Login signup is working but when I am trying to send a reset password Link then is Not sending to email 如何在登录中隐藏导航栏,在反应路由器中注册页面 - How to hide navbar in login, register page in react router
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM