A guide on changing the style of a Layout component in React

  • Currently, I am utilizing Next.js alongside Material-UI as the framework of choice.
  • Within my project, there is a Layout component that encapsulates the content with Material-UI's <Container>.
  • I desire to customize the style of the Layout component in order to remove the width limitations on the background and have it span across the entire screen.

components/Layout.js

import { Container } from '@material-ui/core';

export default function Layout({ children }) {
  return <Container>{children}</Container>;
}

pages/_app.js

import Layout from '../components/Layout';

...
<Layout>
  <Component {...pageProps} />
</Layout>
...

pages/index.js

export default function App() {
  return (
    <div style={{ backgroundColor: "yellow" }}>
      Home Page
    </div>
  )
}

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


Using the Layout component proves useful for most scenarios, but there are instances where I need to supersede specific styles within Layout from the child components.

In this particular situation, how can I override the style of the Layout component that enforces a maxWidth constraint?

I attempted to include {width: '100vw'} within the styles of pages/index.js, however, it was ineffective.

Any guidance on this matter would be greatly appreciated.

Access the SandBox Here

Answer №1

I found a solution to this issue by utilizing the power of React Context.

context/ContainerContext.js

import React from 'react';

const ContainerContext = React.createContext();

export default ContainerContext;

components/Layout.js

import React, { useState } from 'react';
import { Container } from '@material-ui/core';
import ContainerContext from '../context/ContainerContext';

export default function Layout({ children }) {
  const [hasContainer, setHasContainer] = useState(true);

  const Wrapper = hasContainer ? Container : React.Fragment;

  return (
    <ContainerContext.Provider value={{ hasContainer, setHasContainer }}>
      <Wrapper>{children}</Wrapper>
    </ContainerContext.Provider>
  );
}

pages/index.js

import { useContext, useLayoutEffect } from 'react';
import ContainerContext from '../context/ContainerContext';

export default function App() {
  const { setHasContainer } = useContext(ContainerContext);

  // Making the magic happen!
  useLayoutEffect(() => {
    setHasContainer(false);
  }, []);

  return (
    <div style={{ backgroundColor: "yellow" }}>
      Home Page
    </div>
  );
}

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

The onload event for windows does not fire

I am currently working on a project that requires the use of tabbing control with flyingbox. To achieve this, I consulted the following link: After referring to the above link, I made some modifications to my project. I am retrieving details from another ...

Ensure that you decode the URL before making any changes to the location in a

Hi everyone, I'm facing an issue and need your help. Let's say we have a URL like site.com/post?comments=1,2,3,4. When I paste it into the browser address bar, my app opens and decodes the URL to site.com/post?comments=1%2C2%2C3%2C4. How can I re ...

Material UI AppBar fixed position that is not part of a grid container

Hey everyone, I'm really struggling with this issue. I recently set my AppBar to have a position of "fixed". However, now the appbar is no longer contained within its container and instead spans the entire screen. I've been trying to figure it ou ...

"Troubleshooting a matter of spacing in HTML5 body

I've been struggling to eliminate the gap between the top of my webpage and the <div> element. Here's the code snippet causing the issue: <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta name="v ...

Creating a Jquery method to handle multi-line ellipsis for long text

Check out this fiddle I made: http://jsfiddle.net/mupersan82/JYuSY/ This snippet handles multiline ellipsis: $(function() { var ellipsisText = $('.multilineEllipseText'); var textContainer = $('.incidentCellBottomRight').height(); whi ...

How can Cypress tests effectively interact with the React application?

In my current setup, there are four unique Cypress tests identified as test1, test2, and test3. Additionally, one of the tests does not have any specific ID assigned to it. My goal is for the application to trigger three distinct APIs based on the test I ...

The NGX-Graph nodes mysteriously change color to deep black when the third property is applied

My goal is to create a tree structure using ngx-graph and have each node linked to a pop-up using "@material-extended/mde". However, I encountered an issue when trying to define the pop-up content based on each individual node by using the property "data". ...

The implementation of useState delays the application of dynamic styling

I'm currently developing a Next.js application. Here is the code snippet from my client-side component: const [view, setView] = useState<string[]>(["all"]); const story = stories?.holidays; const joke = stories?.jokes; const idiom = s ...

Creating a Drop-Down Button Using HTML, CSS, and JavaScript

I am currently working with the following code: <link rel="preconnect" href="https://fonts.googleapis.com"> <link rel="preconnect" href="https://fonts.gstatic.com" crossorigin> <link href="https://fonts.googleapis.com/css2?family=PT+Sans ...

Send a property as a string, then transform the string back into JavaScript

I am in the process of creating a versatile carousel that will cater to various conditions. Therefore, I need to set the "imageQuery" on my display page as a string and then make it work as executable javascript within my carousel. The query functions pr ...

The module was not found: Unable to locate the file './AppAPI' in the project directory

Despite setting up my react app correctly, I keep encountering an error. I have all my code in a file called App.API.js within the src folder. The issue seems to be originating from the index.js file, where the code is: import App from './AppAPI&apo ...

What are the solutions for tackling the npm error when trying to resolve a package and encountering that it

I encountered a problem while setting up my project using Chakra-UI and React. Upon running npm install, I received the following npm error: npm ERR! code ERESOLVE npm ERR! ERESOLVE could not resolve npm ERR! npm ERR! While resolving: @choc-ui/<a href= ...

Continuously animate a series of CSS properties

Here's the code snippet I'm working with: @keyframes ball1 { 0% { transform: translateX(0px); opacity: 0%; } 50% { opacity: 100%; } 100% { transform: translateX(120px); opacity: 0%; } } @keyframes ball2 { 0 ...

Creating a new project in ASP Net Core Angular using Bootstrap 4 for the SideMenu layout instead of Bootstrap 3

I am currently working on developing a website using Angular6, where I aim to have a vertical navbar for large screens and a top navbar with dropdown functionality for mobile devices. While searching online, I came across several examples that seemed overl ...

What causes the Material UI CheckBox to show up twice upon initial rendering?

I'm experiencing a strange issue with my checkboxes that I just can't seem to solve. When the component initially loads, each checkbox is duplicated. However, on subsequent loads everything works as expected. Below is the code snippet in question ...

What is the best way to incorporate SWRs pagination for effectively managing pagination within a URL structure?

In an attempt to address the lack of pagination handling on the backend in this NextJS application, I proposed passing it as query parameters in the URL, like localhost:3000/patients?page=. My initial approach was close: import React, { useEffect } from & ...

Eliminate the option to display/hide password icon on Safari

Is there a method to eliminate the show/hide icon in Safari that pops up when entering symbols into an input field with type "password"? I was able to achieve this in IE/Edge by incorporating the following CSS rule: input[type=password]::-ms-reveal, input ...

Is there a way to make jQuery Validate display errors within the form elements rather than outside of them?

Given the jQuery Validate basic example provided in the link below, what method can be used to display error messages within form elements whenever feasible (excluding checkboxes)? http://docs.jquery.com/Plugins/validation#source ...

The sequence of execution in React hooks with Typescript

I'm having difficulty implementing a language switching feature. On the home page of my app located at /, it should retrieve a previously set preference from localStorage called 'preferredLanguage'. If no preference is found, it should defau ...

Approach to Using Bootstrap 4 with Intl-Tel-Input

I've been attempting to integrate intl-tel-input with bootstrap 4, but I'm facing an issue where the placeholder for the input is not showing up. I've tried various solutions found online, but none of them seem to work. Here's my HTML ...