Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to limit style to component level in React?

Tags:

css

reactjs

My app tries to show emails. Sometimes the style in the email will affect the app itself.

Right now I am using the package juice to inline style in the email html. However, sometimes it cannot inline correctly. So I try to find other solutions.

I know Angular automatically add some random string in each class to make sure style in one component won't affect other component, is there a same way to do it in React? Or is there other way to limit style to the component level without using iframe? Thanks

The demo shows the p { color: red; } from the email also affects the app itself. In this case, it affects Content in app.

Live demo

class Mail extends Component {
  render() {
    // the style inside is from mail, in real case, it can have tens or even hundreds of different styles
    const mailFromServer = `
      <html>
        <head>
          <style>
            p { color: red; }
          </style>
        </head>

        <body>
          <p>Content in mail</p>
        </body>
      </html>
    `;

    return (
      <div>
        <div dangerouslySetInnerHTML={{__html: mailFromServer}} />
      </div>
    );
  }
}

class App extends Component {
  render() {
    return (
      <div>
        <Mail />

        <p>Content in app</p>
      </div>
    );
  }
}
like image 754
Hongbo Miao Avatar asked Mar 24 '18 01:03

Hongbo Miao


People also ask

How do you style React components?

8 Ways to Style React Components. 1 Javascript. import React from 'react'. import ReactDOM from 'react-dom'. import App from './App'. ReactDOM.render (<App />, document.querySelector ... 2 Javascript. 3 Javascript. 4 Javascript. 5 Javascript. More items

What is inline styling in react?

Inline CSS: In inline styling basically we create objects of style. And render it inside the components in style attribute using the React technique to incorporate JavaScript variable inside the JSX (Using ‘ { }’ )

How do I use headerstyles in React components?

Now headerStyles can be used in any component. Create a separate component that renders a simple heading with these styles. Render this component inside your root component. This way, you can create any number of style objects and either pass them down as props to a component or globally export them so that any component can use them.

How do I add CSS to my react app?

When you use Create React App, webpack will take the imported CSS and add it to a style tag at the top of the file rendered in the browser. If you look at the <head> element in your page source, you’ll see the styles: This means that you can keep the CSS alongside the component and it will be collected together during the build phase.


1 Answers

There are few ways to do this.One of the way would be by passing style to the elements and defining styles as objects. For example

const styles = {
        content: {
            color: '#000',
            backgroundColor: '#fafafa',
        },
    };

class Mail extends React.Component {
    render() {
        return() {
            <p style={{styles.content}}> Content </p>
        }
    }
}

If you really want something scalable then you can use styled-components which for me personally work really nicely and fulfills all your styling needs.

like image 177
Kunal arora Avatar answered Sep 30 '22 20:09

Kunal arora