reactjsreact-link

How to link two separate pages in React?


I have two pages Sign Up and Sign In within a folder named components and I am trying to link sign up page with sign in page using <Link>. But it's giving some error.

components/ SignUp.js

<div className = 'spacing'>Already have an account?&nbsp;<span className = 'highlight'><Link to = {'/SignIn'}>Sign In</Link></span></div>

components/ SignIn.js

import React, { Component } from 'react'
import './SignInStyleSheet.css'

class SignIn extends Component {
       render() {
       return (
            <React.Fragment>
            {/* for pop-up  success */}
            <div class = "pop-up-success">
                <h3> Login Successful.</h3>
            </div>
            {/* for pop-up  error */}
            <div class = "pop-up-error">
                <h3> Incorrect Username/ Password.</h3>
            </div>
            <div class = 'container'>
                {/* for form box */}
                <div class =' window'>
                    <div class = 'bold-line'></div>
                    <div class = 'overlay'></div>
                    <div class = 'content'>
                        <div class = 'welcome'>Welcome Again!</div>
                        <div class = 'subtitle'>Thank you to step forwaerd to save water.</div>
                        <form class = "input-fields">
                            <input type = 'email' placeholder = 'Email' class = 'input-line full-width' value = "" name = "useremail" id = "email" required></input>
                            <input type = 'text' placeholder = 'Street' class = 'input-line full-width' required></input>
                            <input type = 'text' placeholder = 'City' class = 'input-line full-width' required></input>
                            <select id = "select_opt" class = 'input-line full-width' required></select>
                            <input type = 'password' placeholder = 'Password' class = 'input-line full-width' value = "" name = "userpasswd" id = "password" required></input>
                        </form>
                        <div class = 'spacing'>Forgot Password?&nbsp;<span class = 'highlight'><a href = "#">Click Here</a></span></div>
                        <div><button id = 'loginBtn' class = 'ghost-round full-width' type = "button">Log In</button></div>
                        <div class = 'spacing'>New User?&nbsp;<span class = 'highlight'><a href = "sign-up.html">Sign Up</a></span></div>
                    </div>
                </div>
            </div>   
        </React.Fragment>
    )
}
}
export default SignIn

error:

Error: Invariant failed: You should not use outside a


Solution

  • Install react-router-dom using the following command

    npm install --save react-router-dom
    

    create a new file (MainRouter) in your project directory

    import { BrowserRouter, Route, Link } from "react-router-dom"; //import the package
    import SignIn from "../SignIn" //import your signIn page
    import SignUp from "../SignUp" //import your signUp page
    
    function MainRouter(){
        return(
            <BrowserRouter>
                <div className="container">
                    <Switch>
                        <Route path="/signIn" component={SignIn} />
                        <Route path="/signUp" component={SignUP} />
                    </Switch>
                </div>
           </BrowserRouter>
    
        )
    }
    export default MainRouter
    

    Why you need this

    react-router-dom package allows you to dynamically navigate the pages in your browser(client side) without refreshing the page,it a nice way to handle navigation in single page application