Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

React Router Dom

//first install react router
npm install react-router-dom
//or
yarn add react-router-dom
///then write your code like in below in your App js or index.js
import {BrowserRouter , Routes , Route} from 'react-router-dom'
<BrowserRouter>
<Routes>
<Route exact path='/' element={<Home/>} />
<Route exact path='/login' element={<Login/>} />
<Route exact path='/signup' element={<Signup/>} />

</Routes>

</BrowserRouter>
//and now your down if you get error try to close your editor and open it again then you are good to go
Comment

react-router-dom

import { useLocation } from "react-router-dom";

function Locaions() {

  let location = useLocation();

  return (
    <>
    {location.pathname === "/Home" ?
    "Hi i am at the homepage"
    :
    "d-none"
    }
    </>
  );
}
Comment

react router dom

npm install react-router-dom
import {
  BrowserRouter as Router,
  Switch,
  Route,
  Link
} from "react-router-dom";
Comment

react router dom

import { BrowserRouter as Router, Switch, Route, Link} from "react-router-dom";
<Router>
    <Switch>
       <Route path="/" />
     </Switch>
</Router>
Comment

react-router-dom

//basic set-up for react routes 
import React from 'react';
import { BrowserRouter, Routes, Route } from "react-router-dom";

import { Register, Login, ChatApp } from "./Pages";

const app = () => {
  return (
      <BrowserRouter>
          <Routes>
              <Route path="/register" element={ <Register/>} />
              <Route path="/login" element={ <Login/>} />
              <Route path="/" element={ <ChatApp/>} />
//<Routes path="~url of path~" element={ <~react component/page~/>}/>
        </Routes>
      </BrowserRouter>
  )
}

export default app;
Comment

react router dom

import React from "react";
import {
  BrowserRouter as Router,
  Switch,
  Route,
  Link
} from "react-router-dom";

// This site has 3 pages, all of which are rendered
// dynamically in the browser (not server rendered).
//
// Although the page does not ever refresh, notice how
// React Router keeps the URL up to date as you navigate
// through the site. This preserves the browser history,
// making sure things like the back button and bookmarks
// work properly.

export default function BasicExample() {
  return (
    <Router>
      <div>
        <ul>
          <li>
            <Link to="/">Home</Link>
          </li>
          <li>
            <Link to="/about">About</Link>
          </li>
          <li>
            <Link to="/dashboard">Dashboard</Link>
          </li>
        </ul>

        <hr />

        {/*
          A <Switch> looks through all its children <Route>
          elements and renders the first one whose path
          matches the current URL. Use a <Switch> any time
          you have multiple routes, but you want only one
          of them to render at a time
        */}
        <Switch>
          <Route exact path="/">
            <Home />
          </Route>
          <Route path="/about">
            <About />
          </Route>
          <Route path="/dashboard">
            <Dashboard />
          </Route>
        </Switch>
      </div>
    </Router>
  );
}

// You can think of these components as "pages"
// in your app.

function Home() {
  return (
    <div>
      <h2>Home</h2>
    </div>
  );
}

function About() {
  return (
    <div>
      <h2>About</h2>
    </div>
  );
}

function Dashboard() {
  return (
    <div>
      <h2>Dashboard</h2>
    </div>
  );
}
Comment

react router dom

npm i react-router-dom@6.0.2 //Updated React Router Dom
Comment

react router dom

npm i react-router-dom@6.0.2
Comment

react-router-dom

import * as React from "react";
import * as ReactDOM from "react-dom";
import { BrowserRouter } from "react-router-dom";
import "./index.css";
import App from "./App";
import * as serviceWorker from "./serviceWorker";

ReactDOM.render(
  <BrowserRouter>
    <App />
  </BrowserRouter>,
  document.getElementById("root")
);
Comment

react router dom

import React, { Component } from 'react';
 
class App extends Component {
  constructor(props) {
    super(props);
    this.handleClick = this.handleClick.bind(this);
  }
 
  handleClick = (event) => {
      alert(event.type);
  }
 
  render() {
    return (
      <div>
        <div className="text-center">
          <button className="btn btn-secondary" onClick={this.handleClick}>
            Click Me
          </button>
        </div>
      </div>
    );
  }
}
 
export default App;
Comment

react router dom

{["/", "/home"].map((path) => (
          <Route
            exact
            path={path}
            element={
              <Wrapper>
                <Home />
              </Wrapper>
            }
          />
        ))}
Comment

react router dom

npx create-react-app demo-app
cd demo-app
Comment

PREVIOUS NEXT
Code Example
Javascript :: how to routing in react js 
Javascript :: install react router dom 
Javascript :: validate latitude longitude javascript 
Javascript :: check if at least one checkbox is checked 
Javascript :: nuxt router push 
Javascript :: usestate remove from array 
Javascript :: redux template create react app 
Javascript :: javascript caesar cipher 
Javascript :: sort array of object by another value array in javascript 
Javascript :: remove underline from hyperlink react 
Javascript :: shuffle an array of numbers in javascript 
Javascript :: js doubly linked list 
Javascript :: javascript separate string by char 
Javascript :: how to get element of an array in javascript 
Javascript :: req body express 
Javascript :: how to set html label value in jquery 
Javascript :: javascript string comma seprated price to int 
Javascript :: javascript console group 
Javascript :: javascript syntax for check null or undefined or empty 
Javascript :: delete multiple keys from object javascript 
Javascript :: js string to node 
Javascript :: react copy to clipboard button 
Javascript :: node js to int 
Javascript :: fetching foreign key data in sequelize 
Javascript :: get element ref react 
Javascript :: datatable setup 
Javascript :: how to render a new page in node js through express 
Javascript :: (node:5547) UnhandledPromiseRejectionWarning: TypeError [ERR_UNKNOWN_FILE_EXTENSION]: Unknown file extension ".json" 
Javascript :: fill an array of zeroes in js 
Javascript :: cube root of a number in js 
ADD CONTENT
Topic
Content
Source link
Name
1+2 =