How to conditionally apply class names in React

How to conditionally apply class names in React

In this post we will give you information about How to conditionally apply class names in React. Hear we will give you detail about How to conditionally apply class names in ReactAnd how to use it also give you demo for it if it is necessary.

we are going to learn about how to apply class names to the element based on the particular condition in React.

Using the ternary operator

We can add or remove class names conditionally by using the JavaScript ternary operator.

Here is an example:

App.js
import React, { useState } from "react";import "./styles.css";export default function App() {  const [isActive,setActive] = useState(true);  return (    <div className={isActive?"green-box":"red-box"}>      <h1>Hello React</h1>     <button onClick={()=>setActive(!isActive)}>Change color</button>    </div>  );}

In the above code, we have added following ternary condition isActive ? “green-box”:”red-box” to div element.

If isActive property is true, we are applying green-box class to the div element else we are applying red-box.

In some cases, you already have a one class name to the div element and you need to apply other class-names conditionally.

We can do it by using a ternary operator inside a template literal.

App.js
import React, { useState } from "react";import "./styles.css";export default function App() {  const [isActive,setActive] = useState(true);  return (    <div className={'container ${isActive?"green-box":"red-box"}'}>      <h1>Hello React</h1>     <button onClick={()=>setActive(!isActive)}>Change color</button>    </div>  );}

In the above code, there is already a container class in the div element and we are applying green-box or red-box classes conditionally.

Applying class name only if condition is true

If you want to apply a class name only if the condition is true, you can do it by setting a second expression to null or undefined in the ternary operator.

See also 

Change default configuration name of Laravel's created_at and updated_at

In this post we will give you information about Change default configuration name of Laravel's created_at and updated_at. Hear we will give you detail about Change default configuration name of Laravel's created_at and updated_atAnd how to use it also give you demo for it if it is necessary.

In this Laravel PHP Tutorial, I will let you know the use of created_at and updated_at column in a database table.

By default, Eloquent automatically maintain the date time value in created_at and updated_at column on your database table. If you do not want for eloquent to maintain created_at and updated_at columns then disable it by adding following property in your model class :

  1. class Member extends Eloquent {
  2. protected $table='members';
  3. public $timestamps= false;
  4. }
class Member extends Eloquent {

 protected $table = 'members';

 public $timestamps = false;

}

If you want to map Laravel's timestamp from created_at to created_on and updated_at to modified_on then you can override const on your model in following way :

const CREATED_AT = 'created_on';
const UPDATED_AT = 'modified_on';

Now Eloquent will take care of the column "created_on" and "modified_on" on your database table.

How to disable created_at and updated_at timestamps in Laravel Model?

Try this..

Hope this code and post will helped you for implement Change default configuration name of Laravel's created_at and updated_at. if you need any help or any feedback give it in comment section or you have good idea about this post you can give it comment section. Your comment will help us for help you more and improve us. we will give you this type of more interesting post in featured also so, For more interesting post and code Keep reading our blogs

For More Info See :: laravel And github

Example:

import React, { useState } from "react";import "./styles.css";export default function App() {  const [isActive,setActive] = useState(true);  return (    <div className={isActive? "green-box" : null}>      <h1>Hello React</h1>     <button onClick={()=>setActive(!isActive)}>Change color</button>    </div>  );}

Now, the green-box class is applied to the div element only if isActive property is true else nothing is applied.

Hope this code and post will helped you for implement How to conditionally apply class names in React. if you need any help or any feedback give it in comment section or you have good idea about this post you can give it comment section. Your comment will help us for help you more and improve us. we will give you this type of more interesting post in featured also so, For more interesting post and code Keep reading our blogs

For More Info See :: laravel And github

We're accepting well-written guest posts and this is a great opportunity to collaborate : Contact US