0

Why isn't the console log in clean up function ever printed?? Even when I navigate away to another page it still isn't printed.

The interval that was created also DOESN'T continue to run as I navigate to a different page even though I didnt clear it.

App.js

return (
  <Router>
    <>
      {loginStatus && <NavbarTemplate loggedInUserName={loggedInUserName}></NavbarTemplate>}
      <Routes>
        <Route path='/' element={<LandingPage></LandingPage>}></Route>
        <Route path='/login' element={<LoginPage loginFunction={loginFunction} registerUserAccountFunction={registerAccountFunction} currentState='login'></LoginPage>}></Route>
        <Route path='/register' element={<LoginPage loginFunction={loginFunction} registerUserAccountFunction={registerAccountFunction} currentState='signup'></LoginPage>}></Route>
        <Route path='/currency' element={<CurrencyPage></CurrencyPage>}></Route>
        {<Route path='/wallet' element={<WalletPage loggedInUserID={loggedInUserID}> </WalletPage>}></Route>}
      </Routes>
    </>
  </Router>

);

Navigation Bar

    <Navbar  bg="dark" variant="dark">
      <Container>
        <Navbar.Brand href="#home">React-Bootstrap</Navbar.Brand>
        <Navbar.Toggle aria-controls="basic-navbar-nav" />
        <Navbar.Collapse id="basic-navbar-nav">
          <Nav className="me-auto">
            <Nav.Link href="/">Home</Nav.Link>
            <Nav.Link href="/currency">Currency</Nav.Link>
            <Nav.Link href="/wallet">Wallet</Nav.Link>
            <Nav.Link href="/login">Login</Nav.Link>
            <NavDropdown title="Dropdown" id="basic-nav-dropdown">
              <NavDropdown.Item href="#action/3.1">Action</NavDropdown.Item>
              <NavDropdown.Item href="#action/3.2">
                Another action
              </NavDropdown.Item>
              <NavDropdown.Item href="#action/3.3">Something</NavDropdown.Item>
              <NavDropdown.Divider />
              <NavDropdown.Item href="#action/3.4">
                Separated link
              </NavDropdown.Item>
            </NavDropdown>
          </Nav>
        </Navbar.Collapse>
        <Navbar.Toggle />
        <Navbar.Collapse className="justify-content-end">
            <Navbar.Text>
              Signed in as: <a href="#login">{props.loggedInUserName}</a>
            </Navbar.Text>
        </Navbar.Collapse>
      </Container>
  </Navbar>

LandingPage.js

useEffect(() => {
    //Gets new information every 6 seconds
    const intervalCall = setInterval(() => {
        console.log("Lading page 6 seconds call!");
        const currTime = new Date().toLocaleTimeString();
        console.log("Time -> "+currTime);
    }, 60*100);

    return () => {
        console.log("CLEAR INTERVAL");
        // clearInterval(intervalCall);
      };
},[])
Yeo Bryan
  • 331
  • 4
  • 24
  • What navigation library you use ? – Dilshan Nov 26 '22 at 03:38
  • that useEffect is mount in first render, the cleanup function will be execute when your component is removed – elVengador Nov 26 '22 at 03:39
  • @Dilshan Browser router, route and routes. I used bootstrap navigation bar to change the href url – Yeo Bryan Nov 26 '22 at 03:43
  • @elVengador Shouldnt navigating to a different page remove it, else when does it get removed – Yeo Bryan Nov 26 '22 at 03:43
  • 2
    A lot of context still missing, you should really try to recreate this with a basic sandbox and share it. Like where is this effect run? Which component, and whereabouts in the component tree is it? Generally if you navigate within React it should work, though hard navigations or just closing the browser, that won't trigger React unmount functions cos the whole thing is just scrapped by the browser – Jayce444 Nov 26 '22 at 03:46
  • @Jayce444 i have added even more code, basically just click on the navigation bar currency link which opens up the currency page. And from there the use effect is executed – Yeo Bryan Nov 26 '22 at 03:51
  • Your `` never get removed from the component tree. pass `exact` prop to the ` – Dilshan Nov 26 '22 at 03:55
  • @Dilshan i have added it but the cleanup function is still not called – Yeo Bryan Nov 26 '22 at 04:01
  • Can't see any other issue with the provided information. Please create a sandbox & share the link to it. – Dilshan Nov 26 '22 at 04:06
  • @YeoBryan The clean-up callback in your useEffect hook only gets called when your component unmounts. If you type a different URL into your browser, close your browser, etc., the component never gets unmounted _because your browser scraps the entire webpage_. This will automatically remove any intervals you set, so they won't run anymore. It's also not clear why you would need to continue calling the function in your setInterval function after the browser is closed, as your site won't be active on the user's device anymore. – Shaavin Nov 26 '22 at 04:13
  • 1
    So I don't know what `Nav.Link` component is, but given it has an `href` value I'm guessing rather then using the history API to dynamically change routes and render route content accordingly, it's doing hard navigations between pages, i.e. equivalent of entering the URL in the browser URL bar and clicking enter. Those 2 aren't the same, and as I commented above, hard navigations won't call unmount functions. You would need to do something like use `react-router`'s `Link` component and give it a "to" path for navigating – Jayce444 Nov 26 '22 at 04:19
  • @Jayce444 do you want to post this as an answer so that can i mark it as correct? this alongside what i found here https://stackoverflow.com/questions/54843302/reactjs-bootstrap-navbar-and-routing-not-working-together. Helped me resolve my issue. – Yeo Bryan Nov 26 '22 at 04:49

1 Answers1

1

The issue is as Jayce444 has identified, as I was using Nav.link with it's href value to navigate it doesn't unmount the components.

In order to navigate to the different pages of the application whilst still using bootstrap's Nav.link component, it should be written as <Nav.Link as={Link} to={/}>Home</Nav.Link> which allows the bootstrap Nav.Link element to render the Link element but still utilize the bootstrap styling

ReactJS Bootstrap Navbar and Routing not working together

Yeo Bryan
  • 331
  • 4
  • 24