-1

I am new to reactjs and have simple question. I want to pass auth props to Login component and want to update its value from within Login component. How can I pass auth to Login component and update its value from Login component.

Secondly, I have one protected route and only logged-in users can open that component. I added Private method to implement this. Is this right approach of making some components protected ?

import "./App.css";
import {
    BrowserRouter,
    Routes,
    Route,
    Switch,
    Navigate,
    NavLink,
} from "react-router-dom";

import Home from "./pages/Home";
import Login from "./pages/Login";
import RequestDemo from "./pages/RequestDemo";
import ProtectedRoute from "./Protected.Route";

const auth = true; //your logic

const Private = ({ Component }) => {
    return auth ? <Component /> : <Navigate to="/login" />;
};

function App() {
    return (
        <BrowserRouter>
            <Routes>
                <Route exact path="/" element={<Home />} />
                <Route exact path="/Login" element={<Login />} />
                <Route
                    path="/RequestDemo"
                    element={<Private Component={RequestDemo} />}
                />
            </Routes>
        </BrowserRouter>
    );
}

export default App;

GodWin1100
  • 1,380
  • 1
  • 6
  • 14
farazch
  • 21
  • 3
  • There's more than one way for protected route. Yes, you can do it like that as mentioned in code snippet. – GodWin1100 Jul 24 '22 at 13:56
  • Right, can u please tell how i can pass auth from app.js to Login component and update its value in Login component ? – farazch Jul 24 '22 at 14:01
  • Does this answer your question? [How to pass props using element in react-router v6?](https://stackoverflow.com/questions/70443591/how-to-pass-props-using-element-in-react-router-v6) – GodWin1100 Jul 24 '22 at 14:02
  • Question is a little too broad and unfocused. Passing props to `Login` works just like it does anywhere else in React... just pass the props you need to pass. As for protecting routes, see this [answer](/a/66289280/8690857) for explanation and examples. – Drew Reese Jul 24 '22 at 19:11

2 Answers2

0

For auth variable I would suggest using as a State or context, below I have provided an example using context, you can do it with State also

import './App.css';
import { BrowserRouter,Routes, Route, Switch, Navigate, NavLink } from 'react-router-dom';

import Home from './pages/Home';
import Login from './pages/Login';
import RequestDemo from './pages/RequestDemo';
import ProtectedRoute from './Protected.Route';
import { useContext } from 'react';

// In the login Component

const LoginComponent = () => {
  const authContext = useContext(MyContext);

  const handleLogin = () => {
    authContext.onAuthChange(true); // this will make the user login that change the value of auth to true
  }

  return (
    <div>Login JSX</div>
  )
}

const MyContext = React.createContext(null);

const Private = ({Component}) => {
  const authContext = useContext(MyContext);
  return authContext.auth ? <Component  /> : <Navigate to="/login" />
}

function App() {
  const [auth, setAuth] = React.useState(true);

  const handleAuthChange = (newAuthState) => {
    setAuth(newAuthState);
  }

  return (
    <MyContext.Provider value={{
      auth,
      onAuthChange: handleAuthChange
    }}>
      <BrowserRouter>
        <Routes>
          <Route exact path='/' element={<Home />} />
          <Route exact path='/Login' element={<Login />} />
          <Route path='/RequestDemo' element={<Private Component={RequestDemo} />} />
        </Routes>
      </BrowserRouter>
    </MyContext.Provider>
    
    
  );
}

export default App;
Bhavesh Daswani
  • 707
  • 6
  • 11
-1

1. In your case first you have to change auth from const to let as you want to change auth. Then you have to pass auth and an function which can update auth in the same file(where auth is declared) hence you will pass 2 props auth and function which updates auth.
From your code syntax I'm assuming you are using v6.

import "./App.css";
import {
    BrowserRouter,
    Routes,
    Route,
    Switch,
    Navigate,
    NavLink,
} from "react-router-dom";

import Home from "./pages/Home";
import Login from "./pages/Login";
import RequestDemo from "./pages/RequestDemo";
import ProtectedRoute from "./Protected.Route";

let auth = true; // you can mutate let and not constant

const authUpdate = (argument) => {
    // update auth in this function and pass this function to the component
    console.log(auth + 1);
    // if you want some argument or parameter you can do so by declaring argument
    console.log(argument); // only if you want to send some argument or else no need
};

const Private = ({ Component }) => {
    return auth ? <Component /> : <Navigate to="/login" />;
};

function App() {
    return (
        <BrowserRouter>
            <Routes>
                <Route exact path="/" element={<Home />} />
                <Route
                    exact
                    path="/Login"
                    element={<Login auth={auth} authUpdate={authUpdate} />} // passed auth and authUpdate as props
                />
                <Route
                    path="/RequestDemo"
                    element={<Private Component={RequestDemo} />}
                />
            </Routes>
        </BrowserRouter>
    );
}

export default App;

2. There's more than one way for protected route. Yes, you can do it like that as mentioned in code snippet.

PS: I suggest you to use state for auth if you want to trigger re-rendering and want to update auth value at parent and child component level both. If component doesn't re-render's then prop/variable won't be updated

import { useState } from "react";

const [auth,setauth]=useState(true)

const authUpdate=()=>{
    // pre-steps
    setauth(auth=>!auth)
    // you should avoid passing setauth directly as a prop
    // down the line in child component you can forget what setauth does and you can set something different  
}

If you want to use certain variable/state in all child component you can look into Store or Global State concept. You can use react-redux or useContext which does not need to pass props from parent component to child component (prop drilling)

GodWin1100
  • 1,380
  • 1
  • 6
  • 14
  • I am following option 1 which you suggested... How will i access auth in Login component ? i tried the following way but it is not accessing the auth value which i m setting in app.js const Login = (props) => { return (
    {console.log(props.auth)}
    ) }
    – farazch Jul 24 '22 at 16:26
  • I updated the code pass it as such auth={auth}. And I would suggest you to use state based as it will rerender. Pardon I passed it as boolean parameter rather then passing as props. – GodWin1100 Jul 24 '22 at 17:41
  • Why I got downvoted without any positive criticism? – GodWin1100 Jul 25 '22 at 10:47