question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Being able to spread in React {...cx('awesome-style')}

See original GitHub issue

The idea I am playing with is to save a couple of characters of typing . I am not sure if this really something worth pursuing, but it has been somewhat of an annoyance since I often mistype className and react props can quickly build up.

import React from 'react'
import cx from 'classnames/react-spread'

// Only type className once inside the component
const MyComponent = ({ className, ...props }) =>
  <div {...cx(className, 'another-class')} />

// No need to type className at all
const MyOtherComponent = () => <div {...cx('awesome-style')} />

It would work exactly the same as classNames right now, but instead it would return an object:

import cx from 'classnames/react-spread'

const obj = cx('magic') // => { className: 'magic' }
obj.className // => 'magic'

I was thinking of calling it something like classnames/react-spread.

If maintainers think this is a dumb idea, feel free to close this issue.

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:1
  • Comments:5

github_iconTop GitHub Comments

2reactions
jeroenransijncommented, May 15, 2017

@newyork-anthonyng I always get the wrong autocomplete for className. I am currently trying out something like this in combination with glamor:

import { css } from 'glamor'
import isPlainObject from 'lodash.isplainobject'
import cx from 'classnames'
import TextStyles from './TextStyles'

const cn = (...classes) => {
  return {
    className: cx(classes.map(x => {
      return isPlainObject(x) ? css(x).toString() : x
    }))
  }
}

const MyComp = ({ className, ...props }) => <div {...cn(className, TextStyles.heading300)} />

Your example is pretty much what I had in mind.

2reactions
newyork-anthonyngcommented, May 14, 2017

@jeroenransijn I have definitely typed in all variations of className into my JSX (classnames, classname, classNames, className). I wonder if I should use a better linter to catch these 🤔

Also, I think it would be simple to write your own wrapper around classnames, and use that instead. It would look something like:

// wrappedClassname.js
import cx from 'classnames';

function wrappedClassname() {
  const newArgs = Array.prototype.slice.call(arguments);
  return {
    className: cx(newArgs),
  }
}

export default wrappedClassname;
Read more comments on GitHub >

github_iconTop Results From Across the Web

Use Spread Syntax to Overwrite or Add Styles in React
Using variables for inline styles in React is a great way to apply a style pattern to multiple elements in your components. See...
Read more >
spread style in react with css styles - javascript - Stack Overflow
I am looking for a way to override styles of a component using css className, this is what i've tried so far: <div...
Read more >
Next Generation Customer Experience - Oracle
Good CX has to address the broad spread of expectations across channels. In short... Expectations have changed across the spectrum. Customers are more...
Read more >
The best eMTB motor of 2021 – The 8 hottest motors in test
Which ebike motor is best? We tested seven of the most important motors in our group test and tell you what it's all...
Read more >
Perspectives on retail and consumer goods - McKinsey
of life and serious threats to the physical and mental well-being of people everywhere, ... store within Best Buy is a way to...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found