Search code examples
htmlreactjssassstylesheet

How do I apply a stylesheet from external URL to a specific react component


I have several web pages and I have this:

index.html

<!doctype html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <title>React App</title>
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link rel="stylesheet"  href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
    <link rel="stylesheet"  href="https://maxcdn.bootstrapcdn.com/bootswatch/3.3.7/superhero/bootstrap.min.css">
</head>
<body>
<div class="full-height">
    <div id="app" class="full-height">Loading...</div>
</div>
</body>
<script
        src="https://code.jquery.com/jquery-3.2.1.slim.min.js"
        integrity="sha256-k2WSCIexGzOj3Euiig+TlR8gA0EmPjuc79OEeY5L45g="
        crossorigin="anonymous"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
</html>

If i use this

<link rel="stylesheet"  href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<link rel="stylesheet"  href="https://maxcdn.bootstrapcdn.com/bootswatch/3.3.7/superhero/bootstrap.min.css">

in index.html, styles in my entire app are affected but I want to use them in a single component.

I tried to solve this by removing those 2 lines and creating chat.scss

#chat{
  @import url("https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css");
  @import url("https://maxcdn.bootstrapcdn.com/bootswatch/3.3.7/superhero/bootstrap.min.css");
}

//other import statements
import "../../styles/chat.scss"

then i set the id of the chat component to 'chat' and applied the scss

export default class Chat extends React.Component {
render() {
    return (
        <div id="chat">
            <div className="full-height">
                <div className="row">
                    <Nav/>
                </div>
                <div className="row full-height">
                    <div className="col-md-3 full-height">
                        <UserProfile/>
                        <OnlineUsers/>
                    </div>
                    <div className="col-md-9 full-height">
                        <div className="full-height">
                            <Messages/>
                            <MessageInput/>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    );
}

}

This approach has same output as importing the styleshees in index.html. It affects all pages.

My question is how can I only apply those stylesheets for my chat component.


Solution

  • My solution is a workaround I'm not proud of but this is how you can disable a stylesheet dynamically in javascript:

    All stylesheets on the web page can be found inside of the document.styleSheets collection. In order to grab the first one that caused me problems:

    var stylesheet = document.styleSheets[0];
    

    then i simply disabled it in my component:

    stylesheet.disabled = true;