Exploring the potential of Material UI's "sx" prop in combination with computed values

Currently, I am working on a Fab component that changes color when clicked. However, I am facing an issue where the text color can only be modified within the sx property. I am unsure why this conditional cannot be applied inside the sx prop.

If I cannot use the conditional here, how can I override the default value? The className is not allowing me to change the CSS "color" property.

The main objective is to have an active fab button with customized CSS values to make it stand out.

activeFabs.map((item, i) => (
  <Fab
    key={i}
    variant="extended"
    className={classes.navbtns}
    sx={{
      maxHeight: 50,
      minWidth: 120,
      color: clicked === item.value ? 'linear-gradient(45deg, #A900A6, #A900A6)' : 'white',
      marginRight: 3,
    }}
    onClick={handleClick}
    value={item.value}
    name={item.value}
  >
    {item.text}
  </Fab>
));
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.6.3/umd/react.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.6.3/umd/react-dom.production.min.js"></script>

Answer №1

For the code to function properly, the sx must be structured in that specific way.

sx={ clicked === item.value? {color:"red"} : {color:"pruple"} }

Take note of where the curly braces are placed.

Alternatively, if you prefer not to use sx, you can utilize className instead. However, within the CSS file, remember to include !important at the end of the property. For instance:

.a{ color:'red' !important;}

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

Error in TypeScript: The property 'data' is not found within type '{ children?: ReactNode; }'. (ts2339)

Question I am currently working on a project using BlitzJS. While fetching some data, I encountered a Typescript issue that says: Property 'data' does not exist on type '{ children?: ReactNode; }'.ts(2339) import { BlitzPage } from &q ...

Encountering: Unable to break down the property 'DynamicServerError' of 'serverHooks' as it does not have a defined value

An error has arisen in a Nextjs app with TypeScript, specifically in the line of my react component which can be found here. This is my inaugural package creation and after several trials, I managed to test it successfully in a similar vite and TypeScript ...

Capturing user input for LazyList in Jetpack Compose to display dynamically

I've hit a snag trying to display User input in my LazyList. The User's input is collected via the ModelBottomSheet, but despite attempting to save it, I can't get it to show up in my LazyList. I suspect there might be an issue with how the ...

Here's a guide on customizing the appearance of the date picker in NativeBase components for React Native by

Is there a way to show icons while using the date picker component from the Native Base UI library? ...

What is the method for obtaining the size of a mesh object in pixels (instead of Vector3) within BabylonJS?

Exploring Babylon.js How can I retrieve the size of a mesh object in pixels instead of Vector3 in a React application using Babylonjs? This is what I attempted: let meshPixelSize = meshObject.getBoundingInfo().boundingBox; ...

The most effective method for transferring asynchronous data to pages in Next.js

My current directory structure: - components - NavBar - Header - Layout - pages - pages - demo.js - _app.js - index.js // index.js import React from 'react'; import NewLayout from "../../components/NewLayout/NewLayou ...

I would like guidance on how to use onClick to toggle the opening and closing of components, and also how to dispatch a close action

i use createSlice from redux toolkit to create my reducers, where I defined the states below is the code for my store in ./root/app/store.js import { configureStore } from "@reduxjs/toolkit"; import calendarReducer from '../features/calenda ...

next-pwa is failing to generate service worker files

I've been working on a project using react.js and next.js, and I recently decided to turn it into a PWA app by implementing next-pwa. Following the instructions provided in the pwa-next docs, when I try running npm run build, no workbox-*.js or sw.js ...

Challenge with Overriding Material-UI Themes

One of the challenges I faced was overriding the <ExpansionPanelSummary/> component in Material UI to reduce margin. To achieve this, I utilized a theme with overrides. const theme = createMuiTheme({ overrides: { MuiExpansionPanelSummary: { ...

Showcasing or concealing.JSON data depending on the selected item in a React and MaterialUI application

Looking to enhance the user experience, I am developing a dynamic FAQ section sourced from a JSON file containing an array of objects with 'Question' and 'Answer'. https://i.stack.imgur.com/mljpm.png The functionality is such that click ...

What is the best way to establish numerous breakpoints on grid items within Material UI when utilizing React?

My Layout HOC has a Grid with 4 Widget components as Grid items. I want them to display differently based on screen size: On large screens: all 4 should be in 1 row On medium screens: 2 in a row, 2 columns On small screens: 1 widget takes full width, spr ...

React safety: handling state changes

As I dive into my React project, I am amazed by its dynamic nature. However, a recent challenge has arisen when implementing dynamic security features such as Google reCAPTCHA. The fear of users manipulating my states and properties is beginning to worry m ...

The external typing file encounters an issue when trying to locate the relative path to its own index.d.ts file

While working on my project and using react-color as a dependency, I encountered an issue with the tsc import failing. The error message displayed: node_modules/@types/react-color/lib/components/sketch/Sketch.d.ts(2,41): error TS2307: Cannot find module & ...

Cannot read property 'focusVisible' of null error is occurring on the radio

Having a complex setup involving Gatsby 1.9, react-next, React 16.5.2, and Material UI 3.2.2. Encountering issues with Radio and RadioGroup components consistently causing a strange error to pop up (refer to the image below). Unfortunately, unable to upg ...

Unable to insert line breaks using " " or "<br>" in a textarea element

I'm struggling to insert a predefined string with line breaks into a <textarea> using React/NextJS. I've tried various methods, including using &#13; and <br>, but none seem to be effective: setTextAreaValue('a&#13;bc< ...

What is the equivalent of defining conditional string types in Typescript similar to flow?

type UpsertMode = | 'add' | 'update' | 'delete'; interface IUpsertMembers { mode: UpsertMode; } const MagicButton = ({ mode: UpsertMode }) => { return ( <button>{UpsertMode}</button> ); } const Upse ...

How do I retrieve the return value of another function in React?

I am working on a function called HandleCitiesArray where I need to access the myCitiesArray. To achieve this, I want to utilize the useSelector hook from Redux. Specifically, I aim to remove an object from initialState.myCities array. How can I go about ...

Efficient Ways to Reduce Textfield Re-renders

One issue that has caught my attention is the fact that every component within the same parent element (App in the example below) seems to rerender when unrelated states/props change, causing noticeable slowdown in the page/forms. Despite following variou ...

Encountering an Uncaught TypeError due to failure to read the property 'bool' of an undefined value

Encountering a runtime error in my code that I can't seem to pinpoint. Everything was functioning correctly before this error surfaced after updating npm. Here's the error message: Uncaught TypeError: Cannot read property 'bool' of und ...

Recreating the MUI v5 Popper scrolling feature in a CodeSandbox playground

I'm trying to locate the reference code for MUI v5 Popper scroll playground in order to address some issues I'm experiencing with the dynamic arrow within the popper. Unfortunately, the arrow is not behaving dynamically as expected. Click here ...