Giter Club home page Giter Club logo

Comments (6)

peterh32 avatar peterh32 commented on July 28, 2024

Here's what I did @adrianmcli :
Create a file in src: MyComponent.css.js, containing this code

export default ` 
.my_component {
    color: green;
    font-size: 20px;
   ... more css here...
`;

Note the backticks so it exports the css as a big multiline string.

Then in my component js:

import cssStyles from './MyComponent.css';

...and in my render method:

    if (!document.getElementsByTagName('head')[0].querySelector('style[id="my-component"]')) {
      // insert the style into the head
      let tag = document.createElement('style');
      tag.id = 'my-component';
      tag.innerHTML = cssStyles;
      document.getElementsByTagName('head')[0].appendChild(tag);
    }

Voila!

from generator-react-component.

sahilchaddha avatar sahilchaddha commented on July 28, 2024

Is @peterh32 answer valid for adding CSS to React-Components ??

from generator-react-component.

adutta91 avatar adutta91 commented on July 28, 2024

any responses @sahilchaddha ?

from generator-react-component.

sahilchaddha avatar sahilchaddha commented on July 28, 2024

@adutta91 Yes it is, but its not the recommended way. Its a workaround to save css in string format and inject it directly into html. This does not minify CSS.

from generator-react-component.

lucascassiano avatar lucascassiano commented on July 28, 2024

Hi,
Did anyone solve it so far?
also, is it possible to also load .png files?
in my react component (created with create-react-app) I used both css and png

from generator-react-component.

lucascassiano avatar lucascassiano commented on July 28, 2024

Just a note: I kinda solved the .css and .png problem:
-for css:
just copy your .css files into /dist/ and make sure on the README.md page explains how to directly import them, e.g.


Import 'myComponent/dist/css/myStyle.css';

-for png and svg:
Just convert the files to base64 and add them directly to your code, use something like: https://www.base64-image.de for png or http://b64.io for svg to parse the files, I know.. it's not the fastest way, but it works.

from generator-react-component.

Related Issues (12)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.