如何在ReactJS中跳过某些路由的页眉和页脚?

时间:2018-08-11 03:59:34

标签: javascript reactjs react-router

我有以下代码,该代码为所有页面提供一个带有页眉和页脚的应用。

app.js

import React from 'react';
import {
  Route,
  Switch
} from 'react-router-dom';
import { ConnectedRouter } from 'connected-react-router'
import Layout from './components/Layout'
import Home from './homeComponent';
import Login from './loginComponent';
import Dashboard from './dashboardComponent';

const App = ({ history }) => {
  return (
    <Layout>
        <ConnectedRouter history={history}>
            <Switch>
              <Route exact={true} path="/" component={Home} />
              <Route path="/login" component={Login} />
              <Route path="/dashboard" component={Dashboard} />
              ... more routes
              <Route component={NoMatch} />
            </Switch>
        </ConnectedRouter>
    </Layout>
  );
};

export default App;

layout.js

import Header from './headerComponent'
import Footer from './footerComponent'
import React, {Component} from 'react'

class Layout extends Component {
    render() {
        return (
            <div>
                <Header />
                {this.props.children}
                <Footer />
            </div>
        )
    }
}

跳过“首页”和“登录”路线等某些页面的页眉和页脚呈现的最佳方法是什么?

8 个答案:

答案 0 :(得分:1)

我建议创建两个具有各自的页眉和页脚以及专用路线的布局:

公共布局

export const PublicLayout = (props) => <div>
<PublicHeader/>
  <Switch>
    <Route exact path="/" component={HomePage}/>
    <Route exact path='/signin' component={SigninForm} />
    <Route exact path='/signup' component={Signup} />         
  </Switch>
<PublicFooter/>

受保护的布局

export const ProtectedLayout = (props) => <div>
<ProtectedHeader/>
 <Switch>
   <PrivateRoute exact path='/app/dashboard' component={Dashboard} />
   <Route component={NotFound} />
 </Switch>
<ProtectedFooter/>

在app.js中定义高级路由:

export default () => {
  return <div>
    <Switch>
      <Route path='/app' component={ProtectedLayout} />
      <Route path='/' component={PublicLayout} />
    </Switch>
  </div>
}

定义专用路由:

export default ({component: Component, ...rest}) => (
  <Route {...rest} render={props => (
    window.globalState.isAuthenticated() ? (
      <Component {...props} />
    ) : (
      <Redirect to={{
        pathname: '/signin',
        state: {from: props.location}
      }} />
    )
  )} />
)

答案 1 :(得分:1)

是的,我有点晚了。

Visual Studio 2019

import React from 'react';
import { Container } from 'reactstrap';
import NavMenu from '../components/NavMenu';

export default props => (
    <div>
        {window.location.pathname !== '/login' ? <NavMenu /> : null}        
        <Container>
            {props.children}
        </Container>
    </div>
);

我希望有人可以帮忙.. !!!编码愉快

答案 2 :(得分:1)

我在解决问题时提出了一些解决方案。

首先,您可以将Switch包装在网站页眉和页脚中

<BrowserRouter>

    <WebsiteHeader />

    <Switch>
        <Route/>
        <Route/>
        <Route/>
    </Switch>

    <WebsiteFooter/>

<BrowserRouter>

然后在页眉或页脚中使用withRouter中的'react-router-dom'包裹组件,以便您可以访问路线道具

const WebsiteHeader = props => {
    if (props.location.pathname == "/register") return null;
    return (
        <Fragment>
            <DesktopHeader {...props} />
            <MobileHeader {...props} />
        </Fragment>
    );
};

export default withRouter(WebsiteHeader);

答案 3 :(得分:0)

我将创建一些不同的布局,一个带有页眉和页脚,另一个不带布局。然后,而不是将所有内容包装到一个布局中。我只是在每个页面组件中进行包装。因此,您的组件将像:

仪表板组件

<SimpleLayout>
  <Dashboard>
</SimpleLayout>

主页组件

<MainLayout>
  <Home>
</MainLayout>

答案 4 :(得分:0)

使用render

<ConnectedRouter history={history}>
  <Switch>
    <Route path="/dashboard" render={props => <Layout><Dashboard {...props} /></Layout>} />
    <Route path="/login" component={Login} />
  </Switch>
</ConnectedRouter>

答案 5 :(得分:0)

尝试这样

<Route path="/" render={(props) => (props.location.pathname !== "/login") && 
    <Header />}>
</Route>

<Route path="/" render={(props) => (props.location.pathname !== "/login") && 
  <Menu />}> 
</Route>

<PrivateRoute path="/scope" component={Scope}  ></PrivateRoute>

<Route exact path="/login" component={Login} />

在此示例中,我正在检查URL,如果URL为“ / Login”,则将删除 Menu header 组件

答案 6 :(得分:0)

用于强制刷新路由内的 Header。 使用 forceRefresh={true}

const Routing = () => {
    return(
        <BrowserRouter forceRefresh={true}>
        <Header/>
        <Switch>
                <Route exact path="/" component={Home}/>
                <Route path="/list/:id" component={ListingApi}/>
                <Route path="/details/:id" component={HotelDetails}/>
                <Route path="/booking/:hotel_name" component={PlaceBooking}/>
                <Route path="/viewBooking" component={ViewBooking}/>
                <Route exact path="/login" component={LoginComponent}/>
                <Route  path="/signup" component={RegisterComponent}/>
        </Switch>
        <Footer/>
        </BrowserRouter>
    )
}

答案 7 :(得分:0)

强制刷新路由内的Header。

const Routing = () => {
    return(
        <BrowserRouter forceRefresh={true}>
        <Header/>
        <Switch>
                <Route exact path="/" component={Home}/>
                <Route path="/list/:id" component={ListingApi}/>
                <Route path="/details/:id" component={HotelDetails}/>
                <Route path="/booking/:hotel_name" component={PlaceBooking}/>
                <Route path="/viewBooking" component={ViewBooking}/>
                <Route exact path="/login" component={LoginComponent}/>
                <Route  path="/signup" component={RegisterComponent}/>
        </Switch>
        <Footer/>
        </BrowserRouter>
    )
}