question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

How to change the style of components dynamically?

See original GitHub issue

https://www.webpackbin.com/bins/-Kon08wq7y0PT-JC4ufF

This is my current code. Since the stylesheet is static there is no way I can pass props to objects inside it. So my current approach is to use some defined classes like.

const styles = {
  bar: {
    background: '#f00',
  },
  greenBar: {
    background: 'green',
  },
};

and do this

<LinearProgress classes={{ bar: props.classes[props.color] }} />

call using

<Progress color="greenBar" />

It’s not perfect, it’s not even good I think but I would like to know what are the possibilities?

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:13 (6 by maintainers)

github_iconTop GitHub Comments

5reactions
JM-Mendezcommented, Sep 5, 2017

@ConneXNL I know it’s a closed issue, but for anyone stumbling across this, to use react-jss with material-ui, all you have to do is replace material-ui’s withStyles() method with react-jss injectSheet() , and the Themeprovider component in place of MuiThemeProvider.

1reaction
lucas-viewupcommented, May 3, 2018

@oliviertassinari, the point of @dsslimshaddy is a good way to solve that problem but another approach is implement a property that modifies the property of a inner component, in this case, you could implement an property called barProps and using this you could use the property style to modify the style of the inner element:

<LinearProgress barProps={{style: {backgroundColor: anyVariable} }} />
Read more comments on GitHub >

github_iconTop Results From Across the Web

How to change style of styled-component dynamically?
Another simple solution can be with passing the id attribute to your button and querying it on button click. const ButtonsRow = styled.div` ......
Read more >
Make Your CSS Dynamic With Styled Components
import React from "react"; import styled from "styled-components"; const Wrapper = styled.div` font-family: sans-serif; `; const Button ...
Read more >
6 ways to dynamically style Angular components - Medium
The simplest way to update the style of your component is by using ngClass . This directive can dynamically add or change the...
Read more >
How to change the style of components dynamically? #7408
Use CSS variables if you target browsers support it; Use another styling solution; Wait for the upgrade of JSS that support rule' properties...
Read more >
React: How To Change Background Color Dynamically On Click
react #reactjs #webdev #coding #programming how to change background color in react dynamically ? This tutorial will show you How To Change ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found