How does conditional rending of function components in react affect the positioning of my css elements?

I'm in the process of creating a website as part of a project, where I have set up a vertical navigation bar next to the main content area. Utilizing bootstrap, I divided the layout into two main columns - one for the navigation bar and the other for the content, which is displayed using routing. Initially, I applied the "fixed-top" class to the navbar column, ensuring that it stays in place on the viewport even when scrolling.

Now, the navbar includes buttons that direct users to either the login page or their profile, depending on their logged-in status.

In attempting to implement this feature, I experimented with conditional rendering in my navbar function component. Employing an if-else statement, I tried to display the entire navbar with either the sign-in button or the profile and logout buttons based on the user's login status. However, this modification caused the navbar to lose its fixed position and scroll along with the rest of the page.

Could this issue be due to my implementation process? Or is it a limitation of conditional rendering? How can I resolve this issue?

Here is the basic structure of my app functional component:

function App() {
    return (
        <Router>
            <div className="App">
                <Container fluid>
                    <Col className="fixed-top">
                        <Navbar />
                    </Col>
                    <Col>
                        <Routes>
                           All the routing logic goes here
                        </Routes>
                    </Col>
                </Container>
            </div>
        </Router>
    );
}

export default App;

And here is the basic structure of my navbar functional component:

const Navbar = () => {
    // Firebase integration to check user authentication status
    const auth = getAuth();
    
    const [status, setStatus] = useState({
        status: "signed out"
    })

    useEffect(()=>{
        onAuthStateChanged(auth, (user) => {
            if (user) {
              setStatus({
                status: "signed in"
              });
            } else {
              setStatus({
                status: "signed out"
              });
            }
          });
    }, [])

    if(status.status == "signed out") {
        return (
            <Card>
                // Navbar content including website title and logo
               <ButtonGroup vertical>
                    1. Sign in button
               </ButtonGroup>
            </Card>
        );
    } else {
        return (
            <Card>
                // Navbar content including website title and logo
               <ButtonGroup vertical>
                    1. Profile page button
                    2. Logout button
               </ButtonGroup>
            </Card>
        );
    }
}

export default Navbar;

I've explored various methods of conditional rendering, but none have resolved the issue so far. I appreciate any assistance or suggestions on how to tackle this challenge. Thank you!

Answer №1

It is recommended to move the line <code>const auth = getAuth();
outside of the NavBar component for better modularization.

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

How come my "Pop-up box" doesn't appear when clicked a second time?

Although the code above works perfectly when I run it, it causes errors on my website. The issue I'm facing is that on the first click, all links work fine. However, when I try to click a second time, it shows a blank page and I can't click anywh ...

Ensuring consistent placement and scrollability of two divs across all screen sizes

I am in need of a solution to fix the top and bottom divs in the given image. The scroll should only occur when there is overflow. <!DOCTYPE html> <html> <head> <script src="//code.jquery.com/jquery-1.9.1.min.js"></script> ...

Issue with Bootstrap: unable to align columns vertically

I am currently learning how to use bootstrap, but I am facing a challenge with vertical alignment of columns. Despite trying various methods, I can't seem to get the content to align anywhere other than the top. Even starting from scratch with code fr ...

Bootstrap's innovative design for a data grid

I'm currently working on designing a Grid using bootstrap 3. https://i.sstatic.net/DZzcq.png My tools include html5, css, and bootstrap integrated with React.js. The main feature of my project is a data grid (specifically, a Fixed Data Table). ...

Is there a way to retrieve data upon launching each app within nextjs?

I'm currently working on a blog application built with nextjs. I'm looking to retrieve menus (nav) from an API and load them into the Nav component. However, I'm facing a challenge in fetching the menus on each refresh and storing them in th ...

Discovering data from an image URL following text manipulation on a canvas, and displaying the output in Material-UI's CardMedia

Currently, I am navigating through this particular code found on the sandbox here import React, { useEffect, useRef } from "react"; import ReactDOM from "react-dom"; import backImg from "./background.png"; const Canvas = props => { const canvas = us ...

Eliminating a line break that appears after incorporating a list into a paragraph

Currently, I am in the process of developing a website where I need to input information. To display this information, I decided to use a list within a paragraph. However, I noticed that after the list name, the text moves to another line which is not wh ...

Display a selected portion of the background image

I'm currently facing an issue with one of the background images in my div element. The CSS I'm using is as follows: .myBox { background-image:url('....'); background-repeat:no-repeat; background-size:cover; } The background image d ...

My React JS page suddenly turned blank right after I implemented a setState() function within my functional component

I was working on my code and everything seemed fine until I tried to incorporate the setState function with setcategory and setvalue. However, after making this change, my react page suddenly went blank. Can anyone help me identify what went wrong and pr ...

Executing NextJS Request on Each Route Transition

I am working on a project with NEXTJS 13 and a Pages directory. I am looking to make a request to our graphql server every time a route changes. Is it possible to do this from the server-side? Additionally, can I store this data in my Redux store after m ...

Styling text by reducing its size with CSS

Utilizing a CSS class to truncate text in the accordion header, which includes both a URL and plain text fields that need to be displayed on the same line. Desired Output: .../abc/xyz Count:42 Current output: .../abc/xyz Count:42/ (An odd slash is ...

"Exploring the magic of React with webpack's development server

I have a React application and recently ejected to access the webpack configuration files. Where should I make changes for the Webpack dev server? I have come across two files: webpack.config.js and webpackDevServer.config.js. I attempted the following: m ...

Creating a div element with a fixed size that remains the same regardless of its content

Check out the code snippet below for displaying an image and title: <div id="eventsCollapsed" class="panel-collapse collapse" ng-controller="videoController"> <div class="panel-body"> <div ...

React State Displays Incorrect Value of False Instead of True

Task is quite straightforward. Once a checkbox is clicked, the itemOne state is initially set to true. Subsequently, it should switch states if clicked again. Upon invoking itemOneSelected, itemOne is displayed as false. The query: What is the reason behi ...

Blending ThreeJS graphics with typical HTML/CSS pages

Is it feasible to swap out an animated image in the background of a website with JSON geometry while keeping the HTML elements in the forefront? I've attempted to utilize DOM Elements in ThreeJS without success. I experimented with incorporating my JS ...

Why do the props being retrieved from Redux seem to be undefined?

I downloaded the starter template from mern.io and encountered an issue when trying to run the code below. The error message I received was: "ReferenceError: props is not defined" EventDetailPage.js : import React, {PropTypes} from 'react'; imp ...

Identifying the presence of an image in a directory and displaying a standard image if not found

I have a directory containing pictures of different wines, each labeled with a specific code (e.g. FMP-HTR17). I would like to show the corresponding picture if it is available, but display a default image if the specific picture does not exist in the dire ...

The specified CSS background image is not correctly aligned with the dimensions of the selector

I have multiple non-local images that I want to use as background images for classes with a width of 500px and height of 330px. Even though these dimensions are specified in the CSS properties for the class, the background: url('http://www.example.com ...

Transition from Vanilla JS to React is the key to building dynamic and

As a newcomer to react, I've been working on converting my vanilla js parallax effect into react. However, I keep encountering the error "TypeError: Cannot set properties of undefined (setting 'top')". This is the original vanilla js script ...

When the react button is clicked, there is no feedback or message displayed

When I tried to click the button to test if it was clicked or not, I noticed that there was no response when I clicked it. Am I missing something here? import React, { Component } from 'react'; import './App.css'; class App extends ...