skip to Main Content

the button is working but the icon inside it is not working:
enter image description here

the button code:

 <Button color="secondary" aria-label="add an alarm">
<AlarmIcon></AlarmIcon>
</Button>
<Button variant="outlined" startIcon={<AlarmIcon />}>
  Delete
</Button>

the button imports:

import Button from '@mui/material/Button';
import AlarmIcon from '@mui/material/Icon';

the full code example:

import './App.css';
import React, {useState} from 'react';
import Button from '@mui/material/Button';
import AlarmIcon from '@mui/material/Icon';

const App = () => {
  const url = "https://api.quotable.io/random";
  let quoteData = {
    content: "Let time be your only competitor.",
    author: "Ahmed Saber"
  }
  const [quote, setQuote] = useState(quoteData)
 
  const generateQuote = () => {
    fetch(url)
      .then(response => response.json())
      .then(data => {
        console.log(data)
        setQuote(data)
      });
  }

  const copy = () => {
    navigator.clipboard.writeText(quote.author + " once said: " + quote.content)
    alert('copied')
  }

  return (
    <>
      <h1>Quote Generator React App</h1>
      
      <div className="container">
        <p>{quote.content}</p>
        <span>{quote.author}</span>
        <div className="btns">
          <button onClick={copy} className="btn">Copy</button>
          <button onClick={generateQuote}>Generate Another Quote</button>
       
          
        </div>
        <Button color="secondary" aria-label="add an alarm">
<AlarmIcon></AlarmIcon>
</Button>
<Button variant="outlined" startIcon={<AlarmIcon />}>
  Delete
</Button>
      </div>
    </>

  )
  
}


export default App;

3

Answers


  1. Import the IconButton and use it like this:

    import IconButton from '@mui/material/IconButton/IconButton'
    import AlarmIcon from '@mui/icons-material/Alarm'
    
    <IconButton
      aria-label="..."
      size="small"
      onClick={() => ...}
    >
      <AlarmIcon sx={{ color: 'blue' }} />
    </IconButton>
    
    Login or Signup to reply.
  2. Have you tried this?

    <Button
          variant="outlined" 
          InputProps={{
            endAdornment: (
              <AlarmIcon />
            )
            }}
        >
    
    Login or Signup to reply.
  3. Your import statement probably needs to be updated to

    import AlarmIcon from '@mui/icons-material/Alarm';
    

    Using latest Material Icons I had to update the import

    my versions for MUI and MUI icons

        "@mui/icons-material": "^5.14.16",
        "@mui/material": "^5.14.17",
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search