AnswerBun.com

React class components - conditional styling based on props

Stack Overflow Asked by tmc on January 3, 2022

I’m working with older versions of material-ui with no possibility to upgrade.

I am trying to change the background of the Paper component based on a few combinations of the props. I don’t think it’s complicated to require use of the makeStyles HOC. Is this possible?

I think the problem is this line:
classes={{ root: correctBackgroundColor.root }}

but the documentation on the https://v0.material-ui.com/#/components/paper unhelpfully says "Other properties (not documented) are applied to the root element."

import React from "react";

const correctBackgroundColor = {
  root: {
    width: 30,
    height: 30,
    border: "1px solid lightgrey",
    backgroundColor: props => {
      if (props.ledIsOn === true && props.ledColorType === "Green") {
        return "#00FF00";
      }
      if (props.ledIsOn === true && props.ledColorType === "Orange") {
        return "#FFA500";
      } 
    }
  }
};

class ColorChange extends React.Component {
  render() {
    const { classes } = this.props;
    let textToRender = (
      <Paper
        id={this.props.id}
        classes={{ root: correctBackgroundColor.root }}
      />
    );
    return (
      <div>
        <Typography variant="p" className={classes.typography_root}>
          {this.props.textLabel}
        </Typography>
        {textToRender}
      </div>
    );
  }
}

export default withStyles(styles)(ColorChange);

there is a sandbox at : https://codesandbox.io/s/adoring-bell-oyzsn TIA!

One Answer

I hope i got you right. There are two things you should put attention to:

First, correctBackgroundColor does not recognize props because this is out of scope, therefore, I would recomment to change it into a function, pass the props to it, and make the function return a style object.

Second thing, I would use style when applying the object to Paper, so the style of that paper would be a call to correctBackgroundColor with the props, like this:

<Paper id={this.props.id} style={correctBackgroundColor(this.props)} />

Final code:

import React from "react";
import withStyles from "@material-ui/core/styles/withStyles";
import Typography from "@material-ui/core/Typography/Typography";
import Paper from "@material-ui/core/Paper/Paper";

const styles = theme => ({
  typography_root: {
    fontSize: 12,
    margin: 10
  }
});
const correctBackgroundColor = props => {
  let bgSwitch = "none";
  if (props.ledIsOn === true && props.ledColorType === "Green")
    bgSwitch = "#00FF00";
  if (props.ledIsOn === true && props.ledColorType === "Orange")
    bgSwitch = "#FFA500";
  if (props.ledIsOn === true && props.ledColorType === "Red")
    bgSwitch = "#FF0000";
  if (props.ledIsOn === true && props.ledColorType === "Grey")
    bgSwitch = "lightGrey";
  return {
    width: 30,
    height: 30,
    border: "1px solid lightgrey",
    backgroundColor: bgSwitch
  };
};

class ColorChange extends React.Component {
  render() {
    const { classes } = this.props;
    let textToRender = (
      <Paper id={this.props.id} style={correctBackgroundColor(this.props)} />
    );
    return (
      <div>
        <Typography variant="p" className={classes.typography_root}>
          {this.props.textLabel}
        </Typography>
        {textToRender}
      </div>
    );
  }
}

export default withStyles(styles)(ColorChange); //with styles injects classes props with styles contained.

Code Sandbox

Answered by SomoKRoceS on January 3, 2022

Add your own answers!

Related Questions

Update a with react

1  Asked on December 30, 2021 by mara-oliveira

     

Euclidean distance of all pandas rows to single row

1  Asked on December 30, 2021 by aquamad96

     

Different result with online compiler and visual code

0  Asked on December 30, 2021 by swayamjeet-swain

   

recursive function debug asserion failed

2  Asked on December 30, 2021 by steven-zhou

   

SQL Grant Execute On Object where do I put GO?

2  Asked on December 30, 2021 by andy-williams

   

Split string by comma unless followed by a space or a ‘+’

3  Asked on December 30, 2021 by callum-brown

       

Get values of cells in dataframe column quickly

2  Asked on December 30, 2021 by user11035198

     

Parsing XML values in Javascript

2  Asked on December 30, 2021 by topcat3

   

split sequences

1  Asked on December 30, 2021 by backgroup

 

when checkbox checked value true, display text not displayed

4  Asked on December 30, 2021 by user11247496

     

.net core 2.0 logging inside Kubernetes pod console

1  Asked on December 30, 2021 by abhay

       

XML vs JSON vs SQLite for only reading data

9  Asked on December 30, 2021 by brandon-cornelio

   

Ask a Question

Get help from others!

© 2023 AnswerBun.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP