219

I'm doing some React right now and I was wondering if there is a "correct" way to do conditional styling. In the tutorial they use

style={{
  textDecoration: completed ? 'line-through' : 'none'
}}

I prefer not to use inline styling so I want to instead use a class to control conditional styling. How would one approach this in the React way of thinking? Or should I just use this inline styling way?

David L. Walsh
  • 24,097
  • 10
  • 61
  • 46
davidhtien
  • 2,438
  • 2
  • 14
  • 8
  • 1
    I think you might have `redux` and `react` confused. Redux has nothing to do with styling. – rossipedia Mar 03 '16 at 03:04
  • 4
    i think your preference is spot-on for documents, but over-zealous for applications where markup interchange compat is not important. some major web apps are actually getting rid of classes and using only inline style, which is more predictable and easier to reason about than which of 5 applied rules is making the text bold. when the attribs are dynamic, you don't save much bandwidth like you do with repetitive documents. the app's semantics (view-source markup) are not that important either... – dandavis Mar 03 '16 at 04:18
  • @rossipedia ah yes thank you, got mixed up, was looking at the redux tutorial when thinking about this, thank you! – davidhtien Mar 04 '16 at 01:10
  • If you aren't sure what the value of text-decoration will be because of the cascade and you only want to apply a line-through if complete is true, you'll have to build a style object. This way, you don't set it to none accidentally when it was another value. const style = { } if (complete) { style['textDecoration'] = 'line-through' } – Edward Jun 20 '19 at 07:59

16 Answers16

189
 <div style={{ visibility: this.state.driverDetails.firstName != undefined? 'visible': 'hidden'}}></div>

Checkout the above code. That will do the trick.

Abhay Shiro
  • 3,431
  • 2
  • 16
  • 26
141

If you prefer to use a class name, by all means use a class name.

className={completed ? 'text-strike' : null}

You may also find the classnames package helpful. With it, your code would look like this:

className={classNames({ 'text-strike': completed })}

There's no "correct" way to do conditional styling. Do whatever works best for you. For myself, I prefer to avoid inline styling and use classes in the manner just described.

POSTSCRIPT [06-AUG-2019]

Whilst it remains true that React is unopinionated about styling, these days I would recommend a CSS-in-JS solution; namely styled components or emotion. If you're new to React, stick to CSS classes or inline styles to begin with. But once you're comfortable with React I recommend adopting one of these libraries. I use them in every project.

David L. Walsh
  • 24,097
  • 10
  • 61
  • 46
  • Hey there, if you decided to use className as your conditional styling method. Without the `classNames lib`. I advise you to use `undefined` instead of `null`. The `className` property takes as input type a String or undefined - type(String | undefined) -- ⚡️ – twixs Aug 21 '19 at 16:19
  • 5
    @JadRizk even better approach is to not set className at all if you don't have a valid value to set it to. `const attrs = completed?{className:'text-strike'}:{}` then `
  • text to maybe strike
  • ` (spread operator). That way you don't set className at all unless you have a good value to assign. This is an important approach for setting some inline styles where you can't know what the current value is (because it could be set by CSS in a file you may not control). – LinuxDisciple Sep 06 '19 at 00:44
  • @LinuxDisciple if all the fields evaluate to falsey then `classnames` just returns an empty string. This will not be affected by any CSS. – David L. Walsh Sep 06 '19 at 06:54
  • @DavidL.Walsh 21 hours ago I thought JadRizk's solution was a false choice between `null` and `undefined` that would still result in a no-value `class` attribute in the html (i.e. `

    ` instead of `

    `) so I provided a method that avoided setting `className` at all. As it happens I was wrong about JadRizk's solution. For the stated problem, I believe your solution with JadRizk's refinement is best. My syntax can set an arbitrary list of props and their values conditionally, but it's overkill for just setting a class name.
    – LinuxDisciple Sep 06 '19 at 22:51
  • "There's no "correct" way to do conditional styling. Do whatever works best for you. " Thank you, ive been banging my head against the wall trying to find "best practices" – element11 Sep 09 '21 at 02:42