The `<Outlet/>` from react-router is being rendered in a location outside of its intended wrapper div

Utilizing MUI for crafting a straightforward dashboard featuring an <AppBar>, a side <Drawer>, my component appears as follows:

<>
  <AppBar>
// code omitted
  <AppBar/> 
 
  <Drawer>
// code omitted
  <Drawer/>

// along with a wrapper for the Outlet
        <Stack alignItems={'flex-end'} id="router-outlet-wrapper">
            <Toolbar />
            <Box sx={{
                width: `${menuOpen ? `calc( 100% - ${drawerWidth}px )` : '100%'}`,
                height: "100px",
                backgroundColor: 'blue'
            }}>
                Dynamic width div
                <Outlet />
            </Box>
        </Stack>
</>

Upon examining the code, it was discovered that the Outlet correctly renders the div but is positioned outside its parent container. Furthermore, unlike its parent (the blue box), it fails to adjust its width dynamically based on the Drawer's state.

Evidently, it seems hidden behind the Drawer (possibly due to a higher z-index). Take a look at the images below for clarification:

Opened view:

Closed view:

The div housing the text "la page profile" represents the Outlet element in this scenario.

The objective is for it to stretch across the entire page width when the drawer is closed and shrink when open. Currently seeking assistance on this matter.

EDIT 1 :

Below is the minimal-reproducible-example:

https://codesandbox.io/s/outlet-error-mre-zqsk55?file=/src/App.js

Answer №1

I have finally cracked the code! The issue was with how I structured my <Routes>. It turns out, the <PrimaryLayout/> component must have nested routes in order for the <Outlet/> to function properly. Additionally, it needs to be mapped to the root route "/".

I made the following changes to the old code:

<>
  <PrimaryLayout/>
  <Routes>
    <Route path='/' element={<h1>root route "/"</h1>} /> {/* self closing */}
    <Route path='/article' element={<Article />} />
    <Route path='/evenement' element={<Evenement />} />
    <Route path='/profile' element={<Profil />} />
    <Route path='/parametresUtilisateur' element={<ParametresUtilisateurs />} />
  </Routes>
</>

And now, it looks like this:

<>
  <Routes>
    <Route path='/' element={<PrimaryLayout />}> {/* not self closing */}
      <Route path='article' element={<Article />} />
      <Route path='evenement' element={<Evenement />} />
      <Route path='profile' element={<Profil />} />
      <Route path='parametresUtilisateur' element={<ParametresUtilisateurs />} />
    </Route>
  </Routes>
</>

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

What is the method for setting autofocus to the first input element within a loop?

I am currently working on a loop to display inputs, and I would like to be able to add focus to the first input element when it is clicked. Does anyone have any suggestions on how I can select that first element and set autofocus on it? ...

Fetching a Wikipedia page using AJAX or the fetch() method

I am currently attempting to dynamically retrieve a Wikipedia webpage within the browser in order to utilize XSLTProcessor for further processing of the XHTML content. Unfortunately, my efforts have been unsuccessful as Wikipedia is not sending the necess ...

Border becomes dashed when dragging and dropping

I am working on enabling drag and drop functionality for users. When an item is lifted from its original position, a gray dashed box should appear in its place. As the item is moved closer to another spot, the containers adjust to create a target area (gra ...

What is the best method for transmitting server errors to the client?

When using passport.js, I have all the required code in place. However, I am facing an issue with displaying errors on the client-side. Despite having the successRedirect set up correctly, nothing happens when a wrong password is entered, and no error mess ...

Retrieve the page dimensions from a Material UI component `<DataGrid autoPageSize/>`

When utilizing <DataGrid autoPageSize/>, as outlined in the documentation, you can have a Material UI table that adjusts its page size based on the browser height. However, if you are fetching data from the server progressively, it becomes essential ...

Newbie in JavaScript seeking help with JSON indexing: What could be causing my script using (for ... in) to fail?

Not being a seasoned Javascript coder or very familiar with JSON, my approach may seem naive. Any recommendations for better solutions are appreciated. Below is the code I've written: <!DOCTYPE html> <html> <head> <script& ...

What sets these async method declarations apart?

What goes on behind the scenes? const facade = { // A: doSomething: async () => await delegatedFunction(), // B: doSomething: async () => delegatedFunction(), // C: doSomething: () => delegatedFunction(), // D: do ...

Reactjs and Express 'To address this particular file type, you might require a suitable loader.'

I am facing an issue when trying to import bootstrap into my React/Express server. The error message I am getting is as follows: Error ./node_modules/react-bootstrap/esm/Button.js Module parse failed: Unexpected token (18:2) You may need an appropriate lo ...

The button is disabled once inline CSS is applied to it

I was having issues with the CSS of my buttons being overridden by the user agent stylesheet. To fix this, I used inline CSS to override it. However, a new problem emerged where the buttons became unclickable. Here is the code I am using. Any assistance wo ...

Get the characters from a URL following a specific character until another specific character

Having some difficulty using JavaScript regex to extract a specific part of a URL while excluding characters that come after it. Here is my current progress: URL: With the code snippet url.match(/\/[^\/]+$/)[0], I am able to successfully extrac ...

Ways to conceal a div using jQuery when the video source is missing

If there is no source in the video, I want to hide the video_wrapper class <div class="video_wrapper" style="width: 100%; height: 100%; display: none;"> <video id="df-video" playsinline="" webkit-playsinline="" style="height: 100%; width: 100%; ...

The Next.js API is experiencing functionality issues on the Netlify platform

My web application built with Next.js is experiencing issues when deployed on Netlify. The API stops working, although it was functioning correctly on Vercel. Every time I make an API call, I receive a 404 error Here is my package.json: { "name&quo ...

The window.load() function seems to be malfunctioning as the event is not being triggered. There are no error

I'm struggling with getting the window.load() event to trigger on this specific website. The issue arose last night and despite there being no visible js or php errors, pinpointing the root cause has proven to be quite challenging. In syrp.home.main ...

Using jQuery to select elements with specific innerHTML values

$('.select option:selected[innerHTML="5"]') In this case, I am attempting to choose an option that is currently selected and has innerHTML that perfectly matches a specific string (for example: "5") For reference, here is a fiddle link: http:// ...

Sharing a state object with another React file can be accomplished by using props or context to

My first React file makes an API call to retrieve data and save it in the state as Data. import React, { Component } from "react"; import axios from "axios"; import Layout from "./Layout"; class Db extends Component { constructor() { super(); th ...

Modifying the default value setting in MUI Datepicker

Currently, I am utilizing version @mui/x-date-pickers ^6.17.0. Here is the custom date picker I am using: https://i.stack.imgur.com/k8nF1.png Upon clicking the input field, the placeholder switches and a value gets appended. However, modifying the input ...

What methods can I use to test a Django view that includes a form?

As I embark on developing an application using Django, I find myself faced with a particular challenge. I have created a view that receives a form from the HTML code and then searches the database for any instances of a model based on the values specified ...

Exploring the capabilities of searching through a Shopify storefront using GraphQL within a Remix React

As a beginner in Remix React projects, I am currently struggling to grasp the project's structure. My goal is to create a SearchBar Component within the Header that displays the first 10 products based on user input. Below is the code snippet of my Se ...

Controller encountering JSON null data

I am currently working on a feature that allows users to send multiple email/SMS messages by selecting checkboxes. However, I am facing an issue where the data is not being passed correctly from my JavaScript function to the action method - all the data sh ...

Refresh the content of VueJS Resource

Resource file helper/json_new.json { "content": { "content_body": "<a href='#' v-on:click.prevent='getLink'>{{ button }}</a>", "content_nav": "", } } Vue script.js file new Vue({ el: 'body', ...