What is the best way to effectively utilize Material UI breakpoints in combination with styled components?

Here is my code:

const FooterBox = styled(Box)`
  background: #4e738a;
  left: 0;
  right: 0;
  bottom: 0;
  width: 100%;
  color: #ffffff;


  ${p => p?.theme?.breakpoints.up('xs')} {
     margin: auto;
     display: flex;
     flex-direction: column;
     align-items: center;
     justify-content: space-between;
   }

   ${p => p?.theme?.breakpoints.up('md')} {
    margin: auto;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: space-between;
  }
`;

However, TypeScript shows an error:

Property 'breakpoints' does not exist on type 'Theme'.ts(2339)

Answer №1

If you encountered this error, it is likely due to using styled from the @emotion package. To resolve this issue, switch to using styled from the @mui package.

By making this change, you can also address other issues related to the MUI theme in Typescript:

  • Property 'shapes' does not exist on type 'Theme'.ts(2339)
  • Property 'breakpoints' does not exist on type 'Theme'.ts(2339)
  • Property 'transitions' does not exist on type 'Theme'.ts(2339)
  • Property 'palette' does not exist on type 'Theme'.ts(2339)
  • ...
// import styled from '@emotion/styled';
import { styled } from "@mui/material/styles";
import Box from "@mui/material/Box";

const FooterBox = styled(Box)(({ theme }) => ({
  background: "#4e738a",
  left: 0,
  right: 0,
  bottom: 0,
  width: "100%",
  color: "#ffffff",

  [theme.breakpoints.up("xs")]: {
    margin: "auto",
    display: "flex",
    flexDirection: "column",
    alignItems: "center",
    justifyContent: "space-between"
  },

  [theme.breakpoints.up("md")]: {
    margin: "auto",
    display: "flex",
    flexDirection: "column",
    alignItems: "center",
    justifyContent: "space-between"
  }
}));

export default function App() {
  return (
    <div className="App">
      <FooterBox>Hello Wolrd!</FooterBox>
    </div>
  );
}

https://codesandbox.io/s/how-can-i-properly-use-material-ui-breakpoints-with-styled-components-zljggu?file=/src/App.tsx

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

NextJS is currently unable to identify and interpret TypeScript files

I am looking to build my website using TypeScript instead of JavaScript. I followed the NextJS official guide for installing TS from scratch, but when I execute npm run dev, a 404 Error page greets me. Okay, below is my tsconfig.json: { "compilerOption ...

What could be causing the issue with my output not displaying correctly?

Hey guys! I'm working on creating a to-do list but I've encountered a problem. Whenever I enter a value in the text field, it doesn't get added to the array of list elements. Strangely enough, when I console.log it, it seems to work. Can any ...

Stop webpack from stripping out the crypto module in the nodejs API

Working on a node-js API within an nx workspace, I encountered a challenge with using the core crypto node-js module. It seems like webpack might be stripping it out. The code snippet causing the issue is: crypto.getRandomValues(new Uint32Array(1))[0].toS ...

Adding a clear button to the auto complete field in material-ui: a step-by-step guide

I am utilizing the AutoComplete Component (v0.15.4) from Material-UI to create a filtering mechanism. To enhance user experience, I would like to include a clear button (possibly an Icon Button component) either within or beside it that simply clears the c ...

Encountering a SassError while trying to create unique themes with Angular Materials

I'm currently in the process of designing a unique theme for Angular Materials by following the instructions provided in this guide:https://material.angular.io/guide/theming#defining-a-custom-theme However, I've encountered an issue while attemp ...

Loading Material-UI Icons on the fly in React

Seeking help in creating a React component that can dynamically import the requested Material-UI icon on page load. While my current solution works, it triggers warnings during compilation and slows down the project build process. Any suggestions on how t ...

Error encountered while attempting to install material-ui v3.0.3 due to an unexpected termination of the JSON input

I'm currently in the process of installing the most recent stable version of material-ui(v3.03) by running: npm install @material-ui/core. However, I encountered an issue with npm ERR! Unexpected end of JSON input while parsing near '...-/brcast- ...

What is the reason for receiving an error with one loop style while the other does not encounter any issues?

Introduction: Utilizing TypeScript and node-pg (Postgres for Node), I am populating an array of promises and then executing them all using Promise.all(). While pushing queries into an array during iteration over a set of numbers, an error occurs when the ...

Customizing Carousel Arrows in Angular with ng-bootstrap

I need help changing the position and icon of control arrows using Bootstrap. I've tried targeting "carousel-control-prev-icon" & "carousel-control-next-icon", but nothing seems to work. Any suggestions on how to properly solve this issue? Here is th ...

Tips for resolving the "Page Not Found" error in your NextJs application

I am organizing my files in the following structure src/ ├── app/ │ ├── pages/ │ │ ├── authentication/ │ │ │ ├── SignUp/ │ │ │ │ └── page.tsx │ │ │ └── SignIn/ │ ...

`Wrong class names within Material UI when using styled components in a react environment`

Utilizing react-frame-component to load a unique custom component designed with both material ui and styled-components. The specific details of the custom component are not crucial, but here is some pertinent code snippet: const StyledCardHeader = styled( ...

Reactjs Material-UI - Using the useState Hook

Just starting out with ReactjS and attempting to create a multiple select feature export const MuiMultiSelect = () => { const [countries, setCountries] = **useState<string[]>([])**; console.log({countries}) const handleChange = (event) ...

Validating multiple conditions in Typescript by passing them as function parameters

As a beginner in TS/JS, I am looking to validate multiple conditions passed as arguments to a function. For instance, currently I am verifying the user role name, but in the future, I may need to check other conditions. validateUserDetails(): Promise< ...

Eslint for Typescript in Vue is throwing an error with a message "Unexpected token, expecting ','. Make sure to

Whenever I attempted to utilize vue's type assertion, I kept encountering this eslint error. To illustrate, consider the following snippet: data: function () { return { sellerIdToListings: {} as any, // 'as' Unexpected to ...

Is there a way to customize the background color of Material-UI MenuItem when hovering over it through AutoComplete props?

AutoComplete utilizes the Menu component to display MenuItems as demonstrated in the documentation on those respective pages. I am looking to modify the backgroundColor of a MenuItem when it is hovered over. While I have been successful in changing the co ...

Unleashing the Power of Firebase Service in Angular Components: A Guide to Effective Unit Testing

I am currently working on testing my Create-User-Component which relies on an Auth Service that includes methods like 'login, logout,' etc. The Auth Service imports both AngularFireAuth and AngularFirestore, and it is responsible for handling da ...

Using TypeScript, creating a tagged template literal for running Jest tests with the `test.each`

Struggling to construct a jest test.each with a tagged template literal test.each` height | text ${10} | ${undefined} ${20} | ${undefined} ${10} | ${'text'} ${20} | ${'text'} `('$height and $text behave as expected&a ...

Issue with API and Middleware: unable to access /api/auth/providers

Currently, I am following an educational video tutorial on Next Auth V5. Despite being a beginner in coding, I am doing my best to keep up. I am currently stuck at 2 hours, 9 minutes, and 45 seconds into the 8-hour-long video. The issue arises during th ...

Running unit tests using Typescript (excluding AngularJs) can be accomplished by incorporating Jasmine and Webpack

While there is an abundance of resources on how to unit test with Webpack and Jasmine for Angular projects, I am working on a project that utilizes 'plain' TypeScript instead of AngularJs. I have TypeScript classes in my project but do not use c ...

Is it possible to deselect a checkbox in a Datagrid Material-ui by clicking a button?

I have set up a grid in Datagrid Material-UI with a checkbox. I am trying to figure out how to uncheck the checkbox by clicking a reset button after it has been checked. For example: https://i.sstatic.net/Za1E6.png This is the DataGrid code I currently ...