1

I want to give a css class to div based on particular condition. It always takes the last condition.

Here is my code.

I get logos.length as 3

<div className= "${ (this.state.logos && (this.state.logos.length == 3)) ? 'width_15_percent' : (this.state.logos && (this.state.logos.length == 2)) ? 'width_20_percent' : 'width_50_percent' } "> some data </div>

any help would be great.

thank you.

3
  • What is that $ character for? Try to remove that. Commented Dec 27, 2017 at 10:04
  • Your condition seems ok to me. Commented Dec 27, 2017 at 10:04
  • @SergeK.. still it is not working. Commented Dec 27, 2017 at 10:11

7 Answers 7

2

< div className = { (this.state.logos && (this.state.logos.length === 3)) ? 'width_15_percent' : (this.state.logos && (this.state.logos.length === 2)) ? 'width_20_percent' : 'width_50_percent' } > some data < /div>

Sign up to request clarification or add additional context in comments.

13 Comments

whats not working, its working on my system I checked
you must be getting some error, on compile, what's that
it always takes last condition. as I have 3 logos so 1st condition should execute. but it always takes the 3rd condition which is width_50_percent
then its problem with you this.state.logos, the value is not coming properly, because its working fine on my machine
check what is returned in this.state.logos
|
1

The quotations and the $ symbols are not needed here. Pass your expression directly.

<div className={ (this.state.logos && (this.state.logos.length == 3)) ? 'width_15_percent' : (this.state.logos && (this.state.logos.length == 2)) ? 'width_20_percent' : 'width_50_percent' }> some data </div> 

2 Comments

if i remove $ then i get whole syntax in the browser console
I'm sure you're returning the data -- return(...) .And what does the error say? Also your equality symbol should be === not ==, just a react recommendation though
0

<div className={ (this.state.logos && (this.state.logos.length == 3)) ? 'width_15_percent' : (this.state.logos && (this.state.logos.length == 2)) ? 'width_20_percent' : 'width_50_percent' }> some data </div>

Comments

0

If you are using String Literal ($) then you should replace " with `

<div className= `${ (this.state.logos && (this.state.logos.length == 3)) ? 'width_15_percent' : (this.state.logos && (this.state.logos.length == 2)) ? 'width_20_percent' : 'width_50_percent' }`> some data </div> 

Comments

0

Just a suggestion to use classnames - its really simple but efficiently tool to joining classnames or set by condition

Comments

0

There is a great Package for Managing classes check here: https://www.npmjs.com/package/classnames/

Its is very easy to use like

First you have to install and then import like

import classNames from 'classnames' <div className={classNames('yourClassName', { width_15_percent: this.state.logos.length == 3})}> 

Will apply class "width_15_percent" your condition is true

Or If you dont want to use package then go like

<div className={`yourClassName ${(this.state.logos.length == 3) ? 'width_15_percent' : '' }`}> 

Comments

0

For the sake of readability create a method that will just return the correct class instead of writing all these double ternary operators. you could do something like this:

getClassName() { let width = null; switch (this.state.logos.length) { case 2: width = 20 break; case 3: width = 15; break; default: width = 50; break; }; return `width_${width}_percent`; } render() { return ( <div className={this.getClassName()}> ) } 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.