What is the recommended data type for Material UI Icons when being passed as props?

What specific type should I use when passing Material UI Icons as props to a component?

import {OverridableComponent} from "@mui/material/OverridableComponent";
import {SvgIconTypeMap} from "@mui/material";

interface IconButtonProps {
    children: OverridableComponent<SvgIconTypeMap<{}, "svg">> & {muiName: string};
    disabled?: boolean;
}

const IconButton = ({
                        children,
                        disabled = false,
                    }: IconButtonProps) => {
    return (
        <Button
            disabled={disabled}>
            {children}
        </Button>
    )
}

If I try to use it like this:

import AddCircleOutlineIcon from '@mui/icons-material/AddCircleOutline';

<IconButton><AddCircleOutlineIcon/></IconButton>

I encounter the following error:

Type 'Element' is not assignable to type 'OverridableComponent<SvgIconTypeMap<{}, "svg">>'.   Type 'Element' provides no match for the signature '<C extends ElementType>(props: { component: C; } & { children?: ReactNode; classes?: Partial | undefined; color?: "disabled" | ... 8 more ... | undefined; ... 6 more ...; viewBox?: string | undefined; } & CommonProps & DistributiveOmit<...>): Element | null'.

I also attempted:

import {SvgIconComponent} from "@mui/icons-material";
children: SvgIconComponent;

but received the same error message.

Answer №1

It's important to note the distinction between AddCircleOutlineIcon and <AddCircleOutlineIcon />

  • AddCircleOutlineIcon falls under the type
    OverridableComponent<SvgIconTypeMap<{}, "svg">> & {muiName: string};
  • <AddCircleOutlineIcon /> belongs to the type JSX.Element

To address this, consider using the IconButton component from Material UI.

If you prefer to stick with your custom component, update the children type within your IconButtonProps interface to JSX.Element

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 Material UI FilledInput and OutlinedInput components cannot be located within the @material-ui/core library

I recently installed "@material-ui/core": "^1.5.1" and also added material-ui-chip-input@next to my project. When attempting to utilize the material-ui-chip-input component, I encountered an error stating that Module @material-ui/coreFilledInput and @mate ...

Tips for successfully passing an empty value as a prop on a Material UI button

Currently, I am utilizing a ternary operator in a prop for a button. It is functioning as expected, but a warning regarding the empty string keeps appearing in the browser. I am looking for an alternative solution to avoid this error. <Button varia ...

Navigating to a Different Page in Angular 7

After logging in on my login page, the dashboard component appears below the login page instead of redirecting as a new page. How can I achieve this in Angular 7? Any assistance would be greatly appreciated. Thank you! app.component.ts import { Component ...

Angular Tutorial: Understanding the Difference Between TypeScript's Colon and Equal To

I've been diving into the Angular4 tutorial examples to learn more about it. https://angular.io/docs/ts/latest/tutorial/toh-pt1.html One of the code snippets from the tutorial is as follows: import { Component } from '@angular/core'; @Co ...

How can you apply an active class using React-Router?

My React-Router navigation issue nav.tsx import React from 'react' import { menu } from './menu' import { Link } from 'react-router-dom' import styles from './HamburgerMenu.module.scss' const HamburgerMenu: React.F ...

React Native Elements Overlay: Troubleshooting Webview Functionality

I've implemented react-native-element for the components in my app and I'm using Overlay to display modals. However, I encountered an issue with displaying a WebView within one of the modals. export const Modal = ({ visible, showCrossBtn, se ...

Effortless Hydration and Efficient Code Splitting in NextJS Application

I understand Lazy Hydration and Code Splitting, but how can I ensure that the split chunk is only downloaded when the component is being hydrated? This is what my code currently looks like import React from 'react'; import dynamic from 'nex ...

The fade effect in React and material ui is consistent across both elements

I am facing an issue with a list where, for each list sibling onclick, a different element fades in with different text. The problem occurs when the fade effect starts once on siblings in the list and then disappears, not enabling again. This is the code: ...

Enhancing Next.js Images with Custom CSS Styles

I am working with a Next.js component: import styles from '../styles/Navbar.module.css' import Image from 'next/image' export const Navbar = () => { <div> <Image className={styles["navbar-home-icon"]} ...

What is the best way to ensure the image and card maintain the same size ratio in React Bootstrap?

One of the challenges I encountered involved a vertically aligned card group containing a card with an image: https://i.stack.imgur.com/w9Jkf.jpg This is the React code snippet that was causing the issue: import React from 'react'; import { ...

Importing multiple features in Angular

[UPDATE]: Oops, my mind is a bit muddled from fatigue and I've mixed up two ideas which resulted in a rather meaningless question... Let's just blame it on the coffee! :P This may not be a pressing issue but more of a quest for knowledge... ...

Handling type errors with React Typescript MuiAccordion OnChange event handler

I'm a beginner in typescript and seeking advice on defining the type for an event handler. I have a component that utilizes material ui Accordion and triggers the handler from a container. Therefore, I need to specify the type of handleChange in my co ...

having trouble retrieving 200 status code from Angular server response

Objective: I need to make certain changes to the record locally if the server responds with a 200 code. Problem: I am unable to retrieve the response code or access the 'message' attribute. This is the server response I receive from the HTTP ca ...

(Material UI version 5) Custom global style enhancements

One of the examples in MUI is the Global style overrides: const theme = createTheme({ components: { // Name of the component MuiButton: { styleOverrides: { // Name of the slot root: { // Some CSS fontSize ...

The W3C Validator has found a discrepancy in the index.html file, specifically at the app-root location

While attempting to validate my HTML page, I encountered the following error: Error: Element app-root not allowed as child of element body in this context. (Suppressing further errors from this subtree.) From line 4347, column 7; to line 4347, column 16 ...

Personalized Input Field using Material Design UI

Looking to personalize the TextField component in React Material UI, particularly focusing on the following CSS class? root: { '& .MuiOutlinedInput-inputMarginDense': { paddingBottom: 'none !important', }, }, &l ...

Element not producing output via Autocomplete from mui/material package

My challenge involves handling an array of states within the Autocomplete component. Once a state is selected, a corresponding component needs to be rendered based on the selection. However, despite successful state selection in the code, nothing is being ...

A React component enclosed within a useCallback function

According to the React docs, to prevent a Child component from re-rendering in certain cases, you need to wrap it in useMemo and pass down functions in useCallback within the Parent component. However, I'm confused about the purpose of this implementa ...

The pagination in the DataGrid is not aligning correctly to the right when placed within a container with the "rtl" direction attribute

I'm struggling to set up a DataGrid with pagination using Material-UI within a container that has dir="rtl". Despite customizing the pagination component to align to the right side of the container, it doesn't seem to be working correctly. Take ...

Changing the Div heights in Material UI with grid layout customization

I have a project that requires me to implement material-ui. Is there a way I can adjust the width and height of the div containing the "Sign In With" text (as shown in the first image) to bring the buttons closer to the text? Transformation from this: ht ...