Articles → REACT.JS → Add The Style And Classes Dynamically In React.Js
Add The Style And Classes Dynamically In React.Js
Scenario
Code
import React from 'react';
import ReactDOM from 'react-dom'
import '../node_modules/bootstrap/dist/css/bootstrap.min.css';
const Home = () => {
let seconds = (new Date()).getSeconds();
let isEven = seconds % 2 == 0 ? true : false;
return (
<React.Fragment>
<p style={{ color: isEven ? 'red' : 'blue' }}>Heading</p>
<button
className={`btn ${isEven ? "btn-success" : "btn-info"}`}>
Click Me</button>
</React.Fragment >
);
}
ReactDOM.render(<Home />, document.getElementById("root"));
Output
Click to Enlarge