I have an object in which CSS file code is coming like a string. Like
{
css: ".button { color: green } .button:after { color: red }"
}
How can I load CSS as a string in my React Component?
Here is an example of putting css into style tag to be used later
Appending to documnet.head can pollute global css scope; so if that is problematic you can append style to the element you are rendering, therefore the style will be unmounted after component unmounted
function mountStyles ( stylesStr ) {
const styleTag = document.createElement('style');
styleTag.textContent = stylesStr;
document.head.appendChild(styleTag);
}
const someCSS = ".button { color: green } .button:after { color: red } ";
test.addEventListener('click', ()=> {
mountStyles(someCSS);
})
<span class='button'> imagniray button </span>
<button id='test' > Click me </button>
<style>tag to make them accessible; then add appropriate classes to elements you needlet c = { css: ".submit { color: green } .cancel { color: red }" }; let stl = document.createElement("style"); stl.innerText = c.css; document.body.append(stl);