How to switch MaterialUI icon when clicked

material-ui/icons
material-ui button onclick
material-ui iconbutton onclick
material-ui toggle button
material-ui icon button
material-ui fab
material-ui link
material-ui toggle button group

In a list, each list item has an "AddIcon". When clicked I would like to switch that icon to a "BlockIcon" according to it's "id"

import React from 'react'
import {List, ListItem} from '@material-ui/core'
import AddCircleIcon from '@material-ui/icons/AddCircleOutline'
import BlockIcon from '@material-ui/icons/BlockOutlined'

const StackOverflow = () => {
    const handleIconClick = (id) => {
        // change <AddCircleIcon /> to <BlockIcon /> at "id"
    }
    return (
        <List component="nav">
            <ListItem>
                <ListItem button onClick={handleIconClick(101)}>
                    <AddCircleIcon /> 
                </ListItem>
            </ListItem>
        </List>
    )
}

export default StackOverflow

When icon is clicked, I expect the AddIcon to change to a BlockIcon.

You should create a state variable to hold the status of clickec or not clicked you that you can use it to swap the icon

import React from 'react'
import {List, ListItem} from '@material-ui/core'
import AddCircleIcon from '@material-ui/icons/AddCircleOutline'
import BlockIcon from '@material-ui/icons/BlockOutlined'

const StackOverflow = () => {
    const [clicked, setClicked] = useState(false)
    const handleIconClick = (id) => {
         setClicked(true)
        // change <AddCircleIcon /> to <BlockIcon /> at "id"
    }
    return (
        <List component="nav">
            <ListItem>
                <ListItem button onClick={handleIconClick(101)}>
                    {clicked ? <BlockIcon /> : <AddCircleIcon /> }
                </ListItem>
            </ListItem>
        </List>
    )
}

export default StackOverflow

I am also noticing that since you are using a list you will want to maybe click on multiple items and if that is the case then you should rather have it of the form below

const StackOverflow = () => {
    const [clicks, setClicks] = useState([])
     //add the id to the array of clicked items if it doesn't exist but if it does exist remove it. this makes sure that double clicking on an item brings it back to normal
    const handleIconClick = (id) => {
         let result =  clicks.includes(id)? clicks.filter(click => click != id): [...clicks, id]
         setClicks(result)
        // change <AddCircleIcon /> to <BlockIcon /> at "id"
    }
    return (
        <List component="nav">
            <ListItem>
                <ListItem button onClick={handleIconClick(101)}>
                    {clicks.includes(101) ? <BlockIcon /> : <AddCircleIcon /> }
                </ListItem>
            </ListItem>
        </List>
    )
}

export default StackOverflow

In the return statement, Note that if you are looping through the items before displaying then you will have to sent but the id in the clicks.include(id) and handleIconClick(id) will now be using the id not the hard coded number

Is there a way I can overwrite the colour the Material UI Icons npm , How do I change the color of my material UI icon? The icon to display when the component is checked. classes: object: Override or extend the styles applied to the component. See CSS API below for more details. color 'default' | 'primary' | 'secondary' 'secondary' The color of the component. It supports those theme colors that make sense for this component. disabled: bool: If true, the switch

Save the icon id in the component state, and render the icon conditionally:

import React, { useState } from "react";
import { List, ListItem } from "@material-ui/core";
import AddCircleIcon from "@material-ui/icons/AddCircleOutline";
import BlockIcon from "@material-ui/icons/BlockOutlined";

const StackOverflow = (props) => {
  const [iconId, setIconId] = useState(100);

  const handleIconClick = (id) => () => {
    setIconId(id);
  };

  return (
    <List component="nav">
      <ListItem>
        <ListItem button onClick={handleIconClick(101)}>
          {iconId === 100 ?  <AddCircleIcon /> : <BlockIcon />}
        </ListItem>
      </ListItem>
    </List>
  );
};

export default StackOverflow;

How React.js Toggle Button Works - path2code, is to use a custom CSS class name. Suppose that you want to show a green checkbox rather than a red triangle, depending on the outcome of some process. You then apply makeStyles to that function, and run the result. Icon Buttons. Icon buttons are commonly found in app bars and toolbars. Icons are also appropriate for toggle buttons that allow a single choice to be selected or deselected, such as adding or removing a star to an item.

Added a working example(https://codesandbox.io/s/keen-kowalevski-cguvs?fontsize=14)

import React, { useState } from "react";
import ReactDOM from "react-dom";
import IconButton from "@material-ui/core/IconButton";
import AddCircleIcon from "@material-ui/icons/AddCircleOutline";
import BlockIcon from "@material-ui/icons/BlockOutlined";

function App() {
  const [clicked, setClicked] = useState();

  return (
    <IconButton onClick={() => setClicked(true)}>
      {clicked ? <BlockIcon /> : <AddCircleIcon />}
    </IconButton>
  );
}

const rootElement = document.getElementById("root");
ReactDOM.render(<App />, rootElement);

Customizing components, works We need the current state (the initial state which is having the textDisplay: false ) to update the state, therefore passing a function into this. setState is needed, using currentState . the state textDisplay (which is an object) needs to bind with currentState to activate it. ! Switch. Switches toggle the state of a single setting on or off. Switches are the preferred way to adjust settings on mobile. The option that the switch controls, as well as the state it’s in, should be made clear from the corresponding inline label.

Toggle Button React component, Refer to the Icons section of the documentation regarding the available icon options. Component name. The MuiIconButton name can be used for providing default  Refer to the Icons section of the documentation regarding the available icon options. Component name The MuiIconButton name can be used for providing default props or style overrides at the theme level.

IconButton API, The icon to display when the component is checked. classes, object, Override or extend the styles applied to the component. See CSS API below for more details. The API documentation of the Button React component. Learn more about the props and the CSS customization points.

Switch API, Dial Action · Speed Dial Icon · Step · Step Button · Step Connector · Step Content · Step Icon · Step Label · Stepper · Svg Icon · Swipeable Drawer · Switch · Tab  In this tutorial, we are going to learn about how to use the Material UI framework in React apps. Material design is developed by Google in 2014, it uses the grid-based layouts, responsive animations and transitions, padding, and depth effects such as lighting and shadows.

Comments
  • Thank you. That's it!