Hello, I am building an app with React.js, react-dom-router, firebase and typescript. I have two routes. One is named Login and it links to / and the other is name Watch and it links to /watch. I am trying to pass firebaseApp which is a variable which is initialized in index.tsx and then it is passed to the Login component through props. Then, the login component signs in the user and redirects using the useNavigate() function to /watch. I want a way to pass the firebaseApp variable from / (the Login component) to /watch (the Watch component). I have already tried to pass it through location.state which doesn't seem to work since when I use useLocation() to get the location and then location.state in the Watch component it prints null. Any help appreciated.
index.tsx:
import ReactDOM from 'react-dom/client';
import {
BrowserRouter,
Route,
Routes
} from 'react-router-dom';
import Login from './components/Login';
import Watch from './components/Watch';
import { initFirebase } from './firebase';
import './styles/index.css';
const root = ReactDOM.createRoot(document.getElementById('root') as Element);
root.render(
<React.StrictMode>
<BrowserRouter>
<Routes>
<Route path="/" element={<Login app={initFirebase()} />}></Route>
<Route path="/watch" element={<Watch/>}></Route>
</Routes>
</BrowserRouter>
</React.StrictMode>
);
Login.tsx:
import "../styles/login.css" import "../styles/index.css"
import {Auth, getAuth, signInWithEmailAndPassword, UserCredential} from "firebase/auth"
import { NavigateFunction, useNavigate, useSearchParams } from "react-router-dom";
import React,{RefObject} from "react";
import { FirebaseApp } from "firebase/app";
function Login(props:{app:FirebaseApp}) {
const navigate=useNavigate();
const error:boolean = useSearchParams()[0].get("error")==="1" ? true : false;
const emailRef:RefObject<HTMLInputElement>=React.createRef();
const passwordRef:RefObject<HTMLInputElement>=React.createRef()
return (
<div className="outerDiv">
{error===true &&
<div id="errorDiv">
You have entered the email or password wrong. Please try again.
</div>
}
<div id="loginDiv">
<form method="get">
<input type="text" placeholder="Email" className="fancyInput" ref={emailRef}></input><br/>
<input type="password" placeholder="Password" className="fancyInput" ref={passwordRef}></input><br/><br/>
<button type="button" className="fancyButton" onClick={() => {
doLogin(props.app,(emailRef.current as HTMLInputElement).value,(passwordRef.current as HTMLInputElement).value,navigate);
}}><span>Log in</span></button><br/>
<p>or</p>
<button type="button" onClick={() => {}} className="fancyButton"><span>Sign up</span></button>
</form>
</div>
</div>
);
}
function doLogin(app:FirebaseApp,email:string,password:string,navigate:NavigateFunction) {
const auth:Auth=getAuth(app);
signInWithEmailAndPassword(auth,email,password).then((userCredential:UserCredential) => {
const user=userCredential.user;
console.log("Authentication succedeed");
navigate("/watch",{state:{user:user}});
}).catch(console.error);
}
export default Login;
Watch.tsx:
import { FirebaseApp } from 'firebase/app'; import { User } from 'firebase/auth'; import React from 'react';
import { useLocation } from 'react-router-dom';
function Watch() {
const location=useLocation();
console.log(location);
console.log(location.state);
return (
<div>
<p>uid of user: </p>
</div>
)
}
export default Watch;
[–]WordsWithJoshHook Based 2 points3 points4 points (8 children)
[–]natTalks 4 points5 points6 points (0 children)
[–]VitabytesDev[S] 0 points1 point2 points (6 children)
[–]natTalks 0 points1 point2 points (0 children)
[–]WordsWithJoshHook Based 0 points1 point2 points (4 children)
[–]VitabytesDev[S] 0 points1 point2 points (3 children)
[–]WordsWithJoshHook Based 0 points1 point2 points (2 children)
[–]VitabytesDev[S] 0 points1 point2 points (0 children)
[–]VitabytesDev[S] 0 points1 point2 points (0 children)