JSS questionings

While working with CSS in JS (JSS) and material-ui, I have encountered some challenges. I am able to inject styles effectively based on the context, but I am unsure of the additional benefits or features that it offers beyond style injection

For example, when using style injection, I can easily adjust the button style based on different conditions:

const buttonStyle = {
  border: "2px solid black",
  borderRadius: "5px",
  padding: "15px",
  font-family: "15px",
  font-size: "2em",
  backgroundColor: "red"
};

if (success) {
  buttonStyle.backgroundColor = "green";
}

However, with JSS, it seems like I need to pre-define multiple styles for the same button with different color variations:

const style = {
  buttonSuccess: {
    border: "2px solid black",
    borderRadius: "5px",
    padding: "15px",
    font-family: "15px",
    font-size: "2em",
    backgroundColor: "green"
  }, 
  buttonError: {
    border: "2px solid black",
    borderRadius: "5px",
    padding: "15px",
    font-family: "15px",
    font-size: "2em",
    backgroundColor: "red"
  }
};

I am wondering if there is a way to avoid rewriting the entire style when only one parameter needs to be dynamic?

Additionally, it appears that with JSS, we need to assign separate classes for each HTML element that requires styling.

For instance, if I have a table with 200 cells, would I need to add 200 classes to my DOM (compared to defining it only once with a td selector in pure CSS)?

Is there a method to establish an inheritance of styles between parent and children components in JSS? Previously, I have resorted to a cumbersome technique where I merge the injected parent class with the child's compiled style:

const styles = theme => ({
  root: {
    backgroundColor: "blue"
  }
});

const childComponent = (props) => (
  <div className={`${props.parentClass} ${props.classes}`} /> // Parent class mentioned here
);

export default withStyles(styles)(childComponent);

Answer №1

Is it possible to avoid rewriting the entire style when only one parameter is dynamic?

A solution could be using function values. Check out more information here: http://cssinjs.org/json-api?v=v9.8.1#function-values

If I have a table with 200 cells, do I need to add 200 classes in my DOM (when I could declare it once with a td selector in pure CSS)?

You can use the "& td" selector by utilizing the jss-nested plugin, which is already integrated.

http://cssinjs.org/jss-nested?v=v6.0.1

Is there a method to apply inherited styles between parent and children components?

JSS does not alter the inheritance model of CSS. If you are attempting to override a property defined by the core, refer to the customization documentation here: https://material-ui.com/customization/overrides/

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

Tips for enhancing a TypeScript interface for a React component in (Material-UI) by utilizing styled-components

I've been struggling to find a solution for this overload issue with no luck so far. My stack includes Typescript, Styled-components, and Material-UI. I am utilizing styled(MUIButton) to extend the default Button from MUI. While my props are being pas ...

Using React components with Material UI to create styled div boxes

As I work on creating a mock website with React and Material UI, I have utilized the RaisedButtonExampleSimple component for buttons from Material UI within a div named App-Intro. Strangely, the text in the Div "Customer-Choice" is displaying above the but ...

What could be causing the Material UI tabs to malfunction when dynamically populating the content using a .map function instead of manually inserting it?

I was able to successfully integrate Material UI's tabs by manually adding content, but when I attempted to use a .map function to populate the content from a JSON data source, it stopped working. Can someone help me figure out why? The only change I ...

What is the method for performing calculations and retrieving data from an array in this scenario?

The process involves retrieving data from the database and populating an array named rows, which is used to calculate invoiceSubtotal (using the subtotal function), invoiceTaxes, and invoiceTotal. Even though the data is successfully stored in the array, t ...

Every time I switch tabs in Material UI, React rebuilds my component

I integrated a Material UI Tabs component into my application, following a similar approach to the one showcased in their Simple Tabs demo. However, I have noticed that the components within each tab — specifically those defined in the render method ...

Issues with resetting React Material UI TextField when using the useForm hook reset function

Currently, I am working on a project where I am utilizing React and MUI. Within my forms, I have implemented the useForm hook from react-hook-form. Here is a snippet of how I am using it: React.useEffect(() => { const defaultValues = new Provider(); ...

Contrasting the identical tag version of material-ui on GitHub versus the NPM repository

As I was examining the tarball for Material UI version 1.5.1, I noticed that there are actually two different tarballs available for this particular version. Upon comparing the same file from both tarballs, I discovered some discrepancies. One of the tarb ...

Typical ClassIdentifier using Material UI

I have come across similar questions and answers, but due to the MaterialUI update where @mui/styles is deprecated, I didn't fully grasp the concept. As per the MUI docs, we can create custom styles using the sx prop, like this: <Typography varian ...

Tips for customizing the font color in Material UI Typography

Is it possible to change the color of only this text to red? return <Typography style={{ color: 'red' }}>Login Invalid</Typography> I came across this online solution, but I am unsure how to implement it as there is no theme={color ...

How can I intercept/manage the back button of the browser in React-router?

Utilizing Material-ui's Tabs, which are controlled, I am implementing them for (React-router) Links in the following manner: <Tab value={0} label="dashboard" containerElement={<Link to="/dashboard/home"/>}/> <Tab value={1} label="users ...

Using MUI material in Reactjs to apply a ternary condition on an onclick event

I am currently working on developing an event-calendar application using React. The code I have written so far is responsible for adding date items to the calendar list. My goal is to enable users to add more dates when they click on the "add date" button, ...

Using React.js to create a modal that includes ExpansionPanelDetails with a Checkbox feature

I am trying to achieve a specific visual effect with my code. Here is an example of the effect I want: https://i.stack.imgur.com/cJIxS.png However, the result I currently get looks like this: https://i.stack.imgur.com/547ND.png In the image provided, y ...

Guidelines for implementing an onSubmit function using Material-UI

Hey there, I'm relatively new to node.js / react / material-ui. Following a tutorial to build a website has been going smoothly so far. I decided to spice things up by adding material-ui for some sleek components (which weren't covered in the tut ...

How can I configure Material-UI's textfield to return a numerical value instead of a string when the type is set to "number"?

Utilizing Material-UI's TextField alongside react-hook-form to monitor inputs has raised an issue for me. I have observed that regardless of the input type, it always returns a string. This creates conflicts with the data types I am using in my codeba ...

Rendering ListItems in Material UI with React

I seem to be facing an issue when it comes to displaying multiple texts based on a condition in my ListItemText secondary. The goal is to show specific country names if the condition is met, but currently it is not working as expected. Below is the code s ...

How to refresh a page in React when the browser's back button is pressed

In my React project using Material-UI, I have created a simple search form. On page A, users can input values in text boxes and select options from drop-down lists and checkboxes. The results are then displayed on page B. My issue arises when returning to ...

Tips for updating label color when an error occurs in a React application

Can you please explain how to change the color of labels and input fields in error state using Material? Here is what I have tried: <FormControl> <TextField required error ...

Ways to implement a fixed navigation bar beneath the primary navbar using ReactJS

Utilizing ReactJS, I am endeavoring to create a secondary (smaller) navbar in the same style as Airtable's product page. My primary navbar is situated at the top and transitions from transparent to dark when scrolled. The secondary bar (highlighted in ...

How can MakeStyles be used to change the fill color in an SVG file by targeting specific IDs with Selectors?

Consider the following scenario: Contents of SVG file: <g transform="translate(...)" fill="#FFFFFF" id="Circle"> <path ........ ></path> </g> <g transform="translate(...)" fill="#FFFFFF" id="Circle"> &l ...

Is it possible to group rows in the React Data Table Component?

Good day everyone, I am inquiring about how to organize data by name. Can rows be grouped within the react-data-table component? Here is an example of what I mean: https://i.stack.imgur.com/f2sue.png ...