Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Why is my button not firing, bringing me to the profile page?

I think I may be having problems executing the REACT Router. The version of REACT Router DOM in my package.json is "react-router-dom": "^6.6.2". The first component below(App.js) is the code with my Routing. When the button in the second component(the Nav.js) below is clicked it should bring the user to the "profile" page(this component is not listed below). I am not sure if the issue is in the App component that has my routing or possibly in the Nav component that contains the button the routes to "profile".

import React, { useEffect } from 'react';
import HomeScreen from "./HomeScreen";
import ProfileScreen from "./screens/ProfileScreen";
import { BrowserRouter as Router, Routes, Route} from "react-router-dom";`enter code here`
import Login from "./screens/Login"
import { auth } from "./firebase";
import { login, logout } from "./features/userSlice";
import { useDispatch, useSelector } from 'react-redux';
import { selectUser } from "./features/userSlice";
import './App.css';

function App() {

const user =useSelector(selectUser);
const dispatch = useDispatch();



useEffect(()=>{           /*The useEffect will listen to the users logged in state*/
const unsubscribe = auth.onAuthStateChanged((userAuth) =>{     /*This is a listener that 

  if (userAuth) {
   
    dispatch(login({
      uid:userAuth.uid,
      email:userAuth.email,
    }))
  } else{
    //logged out
    dispatch(logout)
  }



});

return unsubscribe;   /*detaching the listener*/
}, [] );






return (
<div className="app">
  <Router>
    {!user ? (
      <Login/>) : (
   
    <Routes>
      <Route  path="/profile" element={<ProfileScreen />}/>
      <Route  path="/" element={<HomeScreen />}/>
    </Routes>
      )}
  </Router>
</div>
);
}

export default App;

Here is my code with the button that is not working.The button is in the second image portion:

import React, { useState, useEffect } from 'react'
import "./Nav.css"
import { useNavigate } from "react-router-dom";

function Nav() {
const [show, handleShow] = useState(false);
const navigate = useNavigate()
const transitionNavBar = () => {
if(window.scrollY > 100) {
    handleShow(true);
} else {
    handleShow(false)
}
};

useEffect(()=>{
window.addEventListener("scroll", transitionNavBar);
return () => window.removeEventListener("scroll", transitionNavBar);/*clean up*/
}, [])


return (
<div className ={`nav ${show && 'nav nav_black'}`}>
    <div className="nav_contents">
        
        <img 
        className="nav_logo"
        src="https://assets.stickpng.com/images/580b57fcd9996e24bc43c529.png" 
        alt=""/>


        <img 
        onClick={() => navigate.push("/profile")}
        className="nav_avatar"
        src="data:image/jpeg
        alt=""/>
        </div>

   

 </div>
 )
 }

 export default Nav

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Do not use navigate.push("/profile"), instead use navigate("/profile"). This worked for me.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading