I have a single page React app. I have a BaseLayout component which serves as the container for the app. BaseLayout displays the header and footer and also the content as shown below:
class App extends Component {
render() {
return (
<div>
<BaseLayout>
</BaseLayout>
</div>
);
}
}
BaseLayout
export default class BaseLayout extends Component {
render() {
return (
<div>
<NavBar />
{this.props.children}
<Footer />
</div>
)
}
}
index.js
ReactDOM.render(
<BrowserRouter>
<Switch>
<Route path="/page_one" component={PageOne} />
<Route path="/page_two" component={PageTwo} />
<Route path="/" component={App} />
</Switch>
</BrowserRouter>,
document.getElementById('root')
)
All the navigation is inside the navBar.js component.
navBar.js
export default class NavBar extends Component {
render() {
return (
<div className="nav-bar">
<button><Link to="/">Home</Link></button>
<button><Link to="/page_one">Page One</Link></button>
<button><Link to="/page_two">Page Two</Link></button>
</div>
)
}
}
pageOne.js and pageTwo.js
export default class PageOne extends Component {
constructor(props) {
super(props)
}
render() {
return (
<div>
<h1>Page One </h1>
</div>
);
}
}
The problem is that when I go to page_one or page_two URL it only shows the content of the component and not the navigation bar.
How can I make the navigation bar and footer visible on all the pages using react router and react framework?
UPDATE:
App.js
class App extends Component {
render() {
return (
<div>
<BaseLayout>
</BaseLayout>
</div>
);
}
}
export default App;
I changed my index.js to the following:
ReactDOM.render(
<BrowserRouter>
<Route path="/" component={App}>
<Switch>
<Route path="/page_one" component={PageOne} />
<Route path="/page_two" component={PageTwo} />
</Switch>
</Route>
</BrowserRouter>,
document.getElementById('root')
)
Now, I get the following:
And the page does not go anywhere if I use the above code:
I think you should "lift" the App as the root Route.
By the way, Is App the same as BaseLayout? hard to tell from your example.
Example:
const Index = () => {
return (
<BrowserRouter>
<div>
<Route component={App} />
<Switch>
<Route path="/page_one" component={PageOne} />
<Route path="/page_two" component={PageTwo} />
</Switch>
</div>
</BrowserRouter>
);
};
A running code example, i'm not using a stack snippet as i can't use react-route with push state here.
I'm posting the entire code in case the url will be broken in the future:
import React from "react";
import { render } from "react-dom";
import { BrowserRouter, Route, Link, Switch } from "react-router-dom";
const Footer = () => <div>Footer...</div>;
class BaseLayout extends React.Component {
render() {
return (
<div>
<NavBar />
{this.props.children}
<hr/>
<Footer />
</div>
);
}
}
class NavBar extends React.Component {
render() {
return (
<div className="nav-bar">
<button>
<Link to="/">Home</Link>
</button>
<button>
<Link to="/page_one">Page One</Link>
</button>
<button>
<Link to="/page_two">Page Two</Link>
</button>
</div>
);
}
}
const PageOne = () => <h1>Page One</h1>;
const PageTwo = () => <h1>Page Two</h1>;
class App extends React.Component {
render() {
return (
<div>
<BaseLayout>{this.props.children}</BaseLayout>
</div>
);
}
}
const Index = () => {
return (
<BrowserRouter>
<div>
<Route component={App} />
<Switch>
<Route path="/page_one" component={PageOne} />
<Route path="/page_two" component={PageTwo} />
</Switch>
</div>
</BrowserRouter>
);
};
render(<Index />, document.getElementById("root"));
Related
This the js file where I have added my router and it contains all the route of my application
router. Below is the code of My App.js
export default class App extends Component {
static displayName = App.name;
render() {
return (
<Router>
<Layout />
<Switch>
<Route path='/employee' exact component={Employee} />
<Route path='/employer' component={Employer} />
<Route path='/addEmployer' component={AddEmployer} />
<Route path='/user' component={UserManagement} />
<Route path='/addUser' component={AddUser} />
</Switch>
</Router>
);
}
}
I have created the Layout page which contains my navbar component, sidebar component, main content and footer too.
Below is the code of Layout.js. I want my layout to be static across the application
import React, { Component } from 'react';
import { Container } from 'reactstrap';
import '../../css/main.css';
import { Navbar } from './Navbar';
import { Footer } from './Footer';
import { SideNavbar } from './SideNavbar';
export class Layout extends Component {
static displayName = Layout.name;
constructor(props) {
super(props);
}
render() {
return (
<div>
<Navbar />
<div id="layoutSidenav">
<SideNavbar />
<div id="layoutSidenav_content">
<main className="flex">
{this.props.children}
</main>
<Footer />
</div>
</div>
</div>
);
}
}
When I am routing the employer from url, the employer component is not rendering under main folder.
currently it is been rendering under the footer of the page. This causing an issue in makin my application as SPA.
Awaiting the response
Issue
Your Layout component looks to be designed to render the content as children, but in your App component you don't pass it any children. The Switch is rendered under/after Layout component.
class Layout extends Component {
static displayName = Layout.name;
constructor(props) {
super(props);
}
render() {
return (
<div>
<Navbar />
<div id="layoutSidenav">
<SideNavbar />
<div id="layoutSidenav_content">
<main className="flex">
{this.props.children} // <-- content
</main>
<Footer />
</div>
</div>
</div>
);
}
}
class App extends Component {
static displayName = App.name;
render() {
return (
<Router>
<Layout /> // <-- no children
<Switch>
<Route path='/employee' exact component={Employee} />
<Route path='/employer' component={Employer} />
<Route path='/addEmployer' component={AddEmployer} />
<Route path='/user' component={UserManagement} />
<Route path='/addUser' component={AddUser} />
</Switch>
</Router>
);
}
}
Solution
Wrap the Switch.
class App extends Component {
static displayName = App.name;
render() {
return (
<Router>
<Layout>
<Switch>
<Route path='/employee' exact component={Employee} />
<Route path='/employer' component={Employer} />
<Route path='/addEmployer' component={AddEmployer} />
<Route path='/user' component={UserManagement} />
<Route path='/addUser' component={AddUser} />
</Switch>
</Layout>
</Router>
);
}
}
Router needs to define where we wanted to render our component. Like In my case it should be under Layout component itself rather the defining at App.js part
I am building an application using react and using react router to manage the routing. I am currently handling the 404 error. My problem is that my application is displaying my navbar and subfooter and footer when at the 404 page i created is displayed. whats the best way to hide these 3 components?
my app.js currently looks like this..
class App extends Component {
render() {
return (
<div className="body">
<div className="App">
<BrowserRouter>
<NavBar />
<Switch>
<Route exact path="/" component={Home} exact={true} />
<Route exact path="/projects" component={Projects} />
<Route component={PageNotFound}/>
<Redirect to="/404" />
</Switch>
<SubFooter/>
<Footer />
</BrowserRouter>
</div>
</div>
);
}
}
you need to create a main route which contain a route for main app (contain nav + contents routes +footer) and a route for 404 page:
const Main = () => (
<BrowserRouter>
<Switch>
<Route path="/404" exact render={() => <div>404</div>} />
<Route path="/" component={App} />
</Switch>
</BrowserRouter>
);
for app js :
import React from "react";
import { Redirect, Route, Switch } from "react-router-dom";
import "./styles.css";
export default function App() {
return (
<div className="App">
<div>navbar</div>
<div>footer</div>
<Switch>
<Route path="/" exact render={() => <div>home</div>} />
<Route exact path="/projects" render={() => <div>projects</div>} />
<Redirect to="/404" />
</Switch>
</div>
);
full example here :example
note : you need to place 404 route before main app route to match first.
You can make a layout for error something like:
layout/Error404.js
import React, { Suspense } from 'react';
export const ErrorLayout = (props) => {
const { children } = props;
return (
<div>
<div>
<Suspense fallback={"loading..."}>
{children}
</Suspense>
</div>
</div>
);
}
layout/publicLayout.js
import React, { Suspense } from 'react';
export const PublicLayout = (props) => {
const { children } = props;
return (
<div>
<NavBar />
<div>
<Suspense fallback={"loading..."}>
{children}
</Suspense>
</div>
<SubFooter/>
<Footer />
</div>
);
}
and create a custom route to render what you need for example:
routes/myErrorRoute.js
import React from "react";
export const ErrorRouteLayout = (props) => {
const { layout: Layout, component: Component, ...rest } = props;
return (
<Route
{...rest}
render={(matchProps) =>
Layout? (
<Layout>
<Component {...matchProps} />
</Layout>
) : (
<Component {...matchProps} />
)
}
/>
);
};
routes/publicRoute.js
import React from "react";
export const PublicRouteLayout = (props) => {
const { layout: Layout, component: Component, ...rest } = props;
return (
<Route
{...rest}
render={(matchProps) =>
Layout? (
<Layout>
<Component {...matchProps} />
</Layout>
) : (
<Component {...matchProps} />
)
}
/>
);
};
and finally, your app.js should look something like this
import { ErrorLayout } from './layout/Error404'
import { PublicLayout } from './layout/publicLayout'
import { ErrorRouteLayout } from './routes/myErrorRoute'
import { PublicRouteLayout } from './routes/publicRoute'
class App extends Component {
render() {
return (
<div className="body">
<div className="App">
<BrowserRouter>
<PublicRouteLayout
component={YourComponent}
exact
path="/home"
layout={PublicLayout}
/>
<ErrorRouteLayout
component={YourComponent}
exact
path="/error404"
layout={ErrorLayout}
/>
<Redirect to="/404" />
</Switch>
</BrowserRouter>
</div>
</div>
);
}
}
I am a beginner in ReactJS. I have been experiencing problems with the code below, which I have written:
class Navigation extends React.Component {
render() {
return (
<div>
<Router>
<button>
<Link to = "/home">Home</Link>
</button>
<button>
<Link to = "/portfolio">Portfolio</Link>
</button>
</Router>
</div>;
)
}
}
class Content extends React.Component {
render() {
return (
<React.Fragment>
<Router>
<Switch>
<Route exact path="/home">
<h1>Home</h1>
</Route>
<Route path="/portfolio">
<h1>Portfolio</h1>
</Route>
</Switch>
</Router>
</React.Fragment>;
)
}
}
class App extends React.Component {
render() {
var element = (
<div>
<Navigation />
<Content />
</div>;
)
return element;
}
}
ReactDOM.render(<App />, document.getElementById('root'));
Problem:
I would like the render() function of Content component to change every time a user clicks on a Link from the Navigation component. How can I go about doing this?
You don't to wrap your Navigation component with Router. As is only the links that will navigate to the component. And another thing is wrap your App component with main BrowserRouter which i guess you have imported as Router here.
class Navigation extends React.Component {
render() {
return (
<div>
<button>
<Link to = "/home">Home</Link>
</button>
<button>
<Link to = "/portfolio">Portfolio</Link>
</button>
</div>;
)
}
}
class Content extends React.Component {
render() {
return (
<React.Fragment>
<Switch>
<Route exact path="/home">
<h1>Home</h1>
</Route>
<Route path="/portfolio">
<h1>Portfolio</h1>
</Route>
</Switch>
</React.Fragment>;
)
}
}
class App extends React.Component {
render() {
return (<Router>
<Navigation />
<Content />
</Router>);
}
}
ReactDOM.render(<App />, document.getElementById('root'));
Hope this works for you.
Hi I googled everywhere about my problem still no luck for me.
Here i attached my sample code.
import React from 'react';
import {
BrowserRouter,
Route,
Switch,
NavLink,
Redirect
} from "react-router-dom";
const Login = () => <div> Login Content <NavLink to ="/AppHome"> go to App</NavLink> </div>;
const Register = () => <div> Register Content </div>;
const AppHome = () => <div> Welcome to the App </div>;
class Authenticate extends React.Component {
render() {
return (
<>
<div>
<NavLink to={"/Login"}> Login </NavLink>
<NavLink to = {"/Register"} > Register < /NavLink>
</div>
<div>
<Switch >
<Route path={"/"} component={Login} />
<Route path={"/Login"} component={Login}/>
<Route path={"/Register"} component = {Register}/>
</Switch>
</div>
</>
);
}
}
class App extends React.Component {
render() {
return (
<BrowserRouter >
<Switch >
<Route path="/" component={Authenticate}/>
<Route path="/AppHome" component={AppHome}/>
</Switch>
</BrowserRouter>
)
}
}
export default App;
Here, In Localhost:3000 , i set Login Component as default to show. it shows the view but when clicking on the signup link, url only changing not the view. what am i done wrong?
You are trying to nest the routes, but in your case this seems unnecessary.
I would setup my routes like this without nested routing:
import React from "react";
import {
BrowserRouter,
Route,
Switch,
NavLink,
Redirect
} from "react-router-dom";
class App extends React.Component {
render() {
return (
<BrowserRouter>
<Authenticate />
<Switch>
<Route path="/" exact component={Login} />
<Route path="/Login" component={Login} />
<Route path="/Register" component={Register} />
<Route path="/AppHome" component={AppHome} />
</Switch>
</BrowserRouter>
);
}
}
const Login = () => (
<div>
Login Content <NavLink to="/AppHome"> go to App</NavLink>
</div>
);
const Register = () => <div> Register Content </div>;
const AppHome = () => <div> Welcome to the App </div>;
class Authenticate extends React.Component {
render() {
return (
<>
<div>
<NavLink to={"/Login"}> Login </NavLink>
<NavLink to={"/Register"}> Register </NavLink>
</div>
</>
);
}
}
export default App;
Codesandbox
import React from 'react'
import ReactDOM from 'react-dom'
import {HashRouter, Route, Switch, NavLink} from 'react-router-dom'
class App extends React.Component {
render() {
return (
<HashRouter>
<Switch>
<Route path="/Login" component={Login} />
<Route path="/Register" component={Register} />
<Route path="/" component={AppHome} />
</Switch>
</HashRouter>
)
}
}
const Login = () => (
<div>
Login Content <NavLink to="/AppHome"> go to App</NavLink>
</div>
)
const Register = () => <div> Register Content </div>
const AppHome = () => <div> Welcome to the App </div>
export default App
ReactDOM.render(<App />, document.getElementById('root'))
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.6.3/umd/react.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.6.3/umd/react-dom.production.min.js"></script>
Can you please check this Codesandbox Once.
I am new to React and trying to create a layout with nested routes. Here's my scenario
show Login when URL is /
show Dashboard when URL is /dashboard
show Profile when URL is /dashboard/profile (this should load
inside the dashboard content area)
The login page and dashboard page are loading properly when the URL is accessed in the browser but for /dashboard/profile, the browser goes to a blank page instead of loading it inside the dashboard component.
Index.js
ReactDOM.render(
<BrowserRouter>
<App />
</BrowserRouter>,
document.getElementById('root'));
App.js
class App extends Component {
render() {
return (
<div>
{/* <Switch> */}
<Route exact path='/' component={SignIn}/>
<Route exact path='/dashboard' component={Dashboard}/>
{/* </Switch> */}
</div>
);
}
}
export default App;
Dashboard.js
class Dashboard extends React.Component {
render() {
const { classes } = this.props;
return (
<React.Fragment>
<CssBaseline />
<div className={classes.root}>
<Header classes={classes} open={this.state.open} click={this.handleDrawerOpen} />
<Sidebar classes={classes} open={this.state.open} click={this.handleDrawerClose} />
<main className={classes.content}>
<div className={classes.appBarSpacer} />
*********I expect profile component to load here
but when I access the URL /dashboard/profile I get a new blank page*********
Route path="/dashboard/profile" exact component={Profile} />
</main>
</div>
</React.Fragment>
);
}
}
You need to remove the exact prop from the Dashboard route (present in Switch) while doing the child routing.
This is the minimal implementation of your use case:
import React, { Component } from "react";
import "./styles.css";
import {
NavLink,
Redirect,
Route,
BrowserRouter as Router,
Switch
} from "react-router-dom";
const App = () => (
<Router>
<div className="App">
<ul>
<li>
<NavLink to="/login">Login</NavLink>
</li>
<li>
<NavLink to="/dashboard">Dashboard</NavLink>
</li>
</ul>
<Switch>
<Route exact path="/login" component={Login} />
<Route path="/dashboard" component={Dashboard} />
</Switch>
</div>
</Router>
);
const Login = () => <span>Login Page</span>;
const Dashboard = () => {
return (
<div>
<div>Dashboard Page</div>
<NavLink to="/dashboard/profile">Go to profile</NavLink>
<div>
<Route exact path="/dashboard/profile" component={Profile} />
</div>
</div>
);
};
const Profile = () => {
return <span>Profile Page</span>;
};
export default App;
You can find the working example here:https://codesandbox.io/s/z3py3672v3