Skip to content

Instantly share code, notes, and snippets.

@amaankulshreshtha
Forked from yamadayuki/SampleComponent.js
Created March 22, 2019 22:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save amaankulshreshtha/2099d9e7d1795abeee1532e299604984 to your computer and use it in GitHub Desktop.
Save amaankulshreshtha/2099d9e7d1795abeee1532e299604984 to your computer and use it in GitHub Desktop.
Use keyframes property with React using inline style
const injectStyle = (style) => {
const styleElement = document.createElement('style');
let styleSheet = null;
document.head.appendChild(styleElement);
styleSheet = styleElement.sheet;
styleSheet.insertRule(style, styleSheet.cssRules.length);
};
export default injectStyle;
import React from 'react';
import injectStyle from './path/to/injectStyle';
export default class SampleComponent extends React.Component {
constructor(props) {
super(props);
const keyframesStyle = `
@-webkit-keyframes pulse {
0% { background-color: #fecd6d; }
25% { background-color: #ef7b88; }
50% { background-color: #acdacf; }
75% { background-color: #87c3db; }
100% { background-color: #fecd6d; }
}
`;
injectStyle(keyframesStyle);
this.state.style = {
container: {
WebkitAnimation: 'pulse 10s linear infinite',
},
title: {
fontSize: '2rem',
},
}
}
render() {
const { style } = this.state;
return (
<div style={style.container}>
<h3 style={style.title}>Hello world using React!</h3>
</div>
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment