Reactjs conditional inline style with className and javascript object
Hello Friends,
In this article we will cover on how to implement react js conditional css. let’s discuss about react js conditional css. it's simple example of react classname multiple conditional. This article will give you simple example of css props.
In react you can use ternary condition in "className" and "style" attribute. you can use condition with state or props.
/src/App.js file
import React from 'react';
import './App.css';
import Mycss from './Mycss';
function App() {
return (
<div>
<Mycss status="active" />
</div>
);
}
export default App;
/src/Mycss.js file
import React from 'react'
import './App.css';
function Mycss(prop) {
const danger ={
color:'red'
};
const success ={
color:'green'
};
return(
<div>
<p className={prop.status == "active" ? "text-success" : "text-danger"}>Conditional Inline Style</p>
<p style={prop.status == "active" ? success : danger}>Conditional Inline Style</p>
</div>
)
}
export default Mycss;
- How to Reactive Forms with Validation in Angular 18?
- Reactjs Props Example With Function and Class Components
- React Color Picker Example With Change Event
- How to Hide and Show Multiple Div in React ?
- React js File Upload Example With Axios
- How to Resize, Crop, Compress Images Before Uploading In ReactJS?
- How to Send Data From React to Node JS Express?
- Reactjs String Replace Example