Nextjs exposes a bug in react-bootstrap's navbar component

After integrating react-bootstrap with nextjs, I encountered a strange issue with the NavBar component. The problem arises when I first load the navbar - everything appears as expected,

https://i.stack.imgur.com/R9guE.png

but upon refreshing the page, CSS-related bugs start to occur,

https://i.stack.imgur.com/cTH1r.png

accompanied by errors in the console.

https://i.stack.imgur.com/e5kED.png

Here is a snippet of my code:

<Navbar collapseOnSelect expand="lg" bg="danger" variant="dark">
  <Container>
    <Link href="/" passHref>
      <Navbar.Brand>Unique Technology</Navbar.Brand>
    </Link>
    <Navbar.Toggle aria-controls="responsive-navbar-nav" />
    <Navbar.Collapse id="responsive-navbar-nav">
      {auth.isLoggedIn && (
        <>
          <Nav className="me-auto">
            <NavDropdown title="Users" id="collasible-nav-dropdown">
              <Link href="/users/" passHref>
                <NavDropdown.Item>Listing</NavDropdown.Item>
              </Link>
              <Link href="/users/create" passHref>
                <NavDropdown.Item>New</NavDropdown.Item>
              </Link>
            </NavDropdown>
          </Nav>
          <Nav className={styles.center}>
            <Navbar.Text>Welcome, {auth.user.name}</Navbar.Text>
            <Nav.Link onClick={logoutHandler}>
              <Button variant="outline-light">Logout</Button>
            </Nav.Link>
          </Nav>
        </>
      )}

      {!auth.isLoggedIn && (
        <Nav>
          <Link href="/login" passHref>
            <Nav.Link>
              <Button variant="outline-light">Login</Button>
            </Nav.Link>
          </Link>
        </Nav>
      )}
    </Navbar.Collapse>
  </Container>
</Navbar>

Can anyone shed light on why this issue is occurring?

Edit: Code related to auth.isLoggedIn:

import { createContext, useState, ReactNode } from "react";
import { useRouter } from "next/router";

interface IAuthContext {
  isLoggedIn: boolean;
  token: string | null;
  user: IUserData;
  onLogout: () => void;
  onLogin: (data: LoginData) => void;
}

// More code and explanation here...

Answer №1

According to juliomalves, moving the inititalToken logic into a useEffect() hook was successful. Note: The initial token variable has been removed, and now the token is set directly with the value from local storage (the initial state of the token variable is now "")

useEffect(() => {
   setToken(localStorage.getItem("token"));
}, []);

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Why does my value always revert to 0 whenever I switch screens while utilizing async storage?

Utilizing a stack Navigator, my primary screen is tracker.js, and the secondary one is macros.js. In macros.js, I have the ability to manually input nutritional macros (calories, Fats, Carbs, Protein) and add them to my UsedDailyCalories. However, when I ...

Using ArcGIS Mapping technology and incorporating NextJS for rendering

Struggling to resolve a persistent rendering issue in my NextJS and Arcgis JS API project. I've searched extensively online for a solution with no luck so far. Here's the deal: I have a simple app set up on this codesandbox, where a counter incre ...

jQuery UI smooths out the transitions, making the effect more gradual and fluid

Is there a way to make my toggle sidebar scroll smoothly using jQuery UI? Currently, it has a jerky behavior and I would like it to have a smoother left/right effect. I want the outer shell to slide smoothly just like the inner container does, instead of ...

Issue encountered with the local mongo connection, but smooth sailing on Vercel in the production environment

I'm currently facing an issue where my MongoDB database successfully connects in production, but encounters connection errors when trying to connect in localhost. Let me walk you through my setup: Backend (lib/mongodb.js): import { MongoClient } from ...

Guide to passing parameters and setting the target of Input in React

Is there a way to pass parameters and the target of Input simultaneously? handleChange (e, a) => { this.setState({ price: e.target.value }); this.callback(e.target.value, a); } ... <input type = 'text' value = {this.state.price} onCha ...

Tips for integrating search functionality into material-ui data grids

I'm in the process of incorporating a quick filter/search feature into my Material-UI datagrid. Currently, the datagrid does not have this functionality built-in. To address this, I am utilizing the Material-UI-Search-Bar library to implement a search ...

Error: Next.js is throwing a SyntaxError due to encountering an unexpected token 'export'

I encountered an issue when trying to render the following code: SyntaxError: Unexpected token 'export' (project path)/node_modules/react-syntax-highlighter/dist/esm/styles/prism/index.js Everything seems to work as expected initially, but then ...

What could be the reason that the painting application is not functioning properly on mobile devices?

I am facing an issue with my painting app where it works perfectly on desktop browsers but fails to function on mobile devices. I tried adding event listeners for mobile events, which are understood by mobile devices, but unfortunately, that did not solve ...

What is the process for creating custom styles for MUIV5 components?

I have incorporated Mui v5 components into my project and currently I am utilizing sx props to style all the MUI components. However, it is becoming cumbersome to add sx in every line for each component. Therefore, I am seeking alternative methods for styl ...

Microsoft Edge and Google Fonts display strong and distinctive lettering

When using Microsoft Edge on Windows 10, all text appears with bold UTF-8 characters: I'm unsure of what the issue is. Is there a solution available? ...

Aligning Checkbox Labels for Web Forms

Can an image convey more than a thousand words? I'm looking to align the second (and following) lines with the first one. Any suggestions? Html stands for <input><span>text</span> ...

server running on node encountered an error due to a port that is already in use

The Server instance emitted an 'error' event at: at emitErrorNT (net.js:1340:8) at processTicksAndRejections (internal/process/task_queues.js:84:21) { code: 'EADDRINUSE', errno: 'EADDRINUSE', syscall: 'listen', addre ...

Remove the uploaded image using Material UI

I'm currently working on implementing an image upload feature using Material UI. I've run into an issue with deleting the uploaded images (handleDelete function). Feel free to take a look at my code here: https://codesandbox.io/s/material-ui-con ...

Creating interactive button groups with responsive design in a Material UI and ReactJS web application

Is it possible to make ButtonGroup Buttons responsive? I heard about an attribute called "Orientation" in material-ui's ButtonGroup, but I'm not sure how to use it with media queries for changing orientation based on the device width. I'm st ...

What are some top tips for setting up a solidity contract with react?

For the past 3 months, I have been delving into solidity and exploring its intricacies. However, I now find myself facing a dilemma on how to effectively link my contract with frontend technologies such as React or Next. Each developer has their unique a ...

Is there a method to implement a pop-in animated contact form without the use of Javascript or query selectors?

Although I was successful in creating a contact form with this functionality using Javascript, I encountered some difficulties when attempting to achieve the same result without it. My goal is for the form to slide in from the right when the "open" icon is ...

What is the best way to include start and end labels on Bootstrap's progress bars?

Is there a way to add start and end labels directly below the progress bar in Bootstrap? Here is an example of what I want: [||||||||------------] <Start Label> <End Label> I have been searching for a solution, but it seems ...

Steps to host a Next.js 13 application on an IIS Server 10

Setting up iisnode and rewriting paths. Successfully deploying a next.js app on iis 10. Various resources provide different explanations. Is it necessary to configure iisnode and rewrite rules? The web.config settings can vary significantly. After numer ...

Activate vertical scrolling in JavaScript when an image is clicked

I currently have a collection of button images with different hover effects specified like this: <img src="home.PNG" onmouseover="this.src='homemo.PNG'" onmouseout="this.src='home.PNG'" /> My goal is to make them scroll down to ...

"Utilizing the power of mapping in React to dynamically generate and return an

Hello everyone! I'm currently working on a project where I am making a get request to a Google API and fetching some data. Initially, the state value is empty, but after receiving the ajax response, I expect the state values to be updated using setSta ...