Comments (6)
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.
Is @peterh32 answer valid for adding CSS to React-Components ??
from generator-react-component.
any responses @sahilchaddha ?
from generator-react-component.
@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.
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.
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)
- Question: How can I easily publish the examples/dist in gh-pages? HOT 2
- NODE_ENV is not a command in windows when npm run build
- Getting babel error:
- Exporting Multiple Components
- Does not work with Node v8 HOT 2
- using ES6 'fat arrow' function
- Question: LESS?
- Question? JSX
- Would you like a default setup for tests? HOT 17
- Gulp
- Tag already exists error when running 'npm run release'
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from generator-react-component.