React Route without link

0

I'm starting my studies with React and I'm using the React Router to navigate between pages by clicking links in the menu, but I have a question, how do I access a page without a link? How do I set up a route in this case?! The project I created using the create-react-app and the route I want to configure is / login to load the class Login.js .

Main.js code with menu:

import React, { Component } from "react";
import {
  Route,
  NavLink,
  HashRouter
} from "react-router-dom";
import Home from "./Home";
import Stuff from "./Stuff";
import Contact from "./Contact";


class Main extends Component {
  render() {
    return (
    <HashRouter>
        <div>
        <h1>Simple SPA</h1>
        <ul className="header">
            <li><NavLink exact to="/">Home</NavLink></li>
            <li><NavLink to="/stuff">Stuff</NavLink></li>
            <li><NavLink to="/contact">Contact</NavLink></li>
        </ul>


        <div className="content">
            <Route exact path="/" component={Home}/>
            <Route path="/stuff" component={Stuff}/>
            <Route path="/contact" component={Contact}/>
        </div>
        </div>
    </HashRouter>
    );
  }
}

export default Main;

Login.js Code that I want to configure the route and direct access through the URL:

import React, { Component } from "react";

class Login extends Component {
    render() {

    return (
        <div>
            <form>
                <label>
                    Name:
                <input type="text" name="name" />
                </label>
                <input type="submit" value="Submit" />
            </form>
        </div>
    );
    }
}

export default Login;
    
asked by anonymous 21.10.2018 / 19:50

1 answer

1

You should first import the login into Main.js
Main.js

import Login from "./Login";

Then even within your Main.js you should add the route

<div className="content">
    // Rota login adicionada
    <Route exact path="/login" component={Login}/>

    <Route exact path="/" component={Home}/>
    <Route path="/stuff" component={Stuff}/>
    <Route path="/contact" component={Contact}/>
</div>
    
23.10.2018 / 13:35