Being able to spread in React {...cx('awesome-style')}
See original GitHub issueThe 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:
- Created 6 years ago
- Reactions:1
- Comments:5
Top 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 >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
@newyork-anthonyng I always get the wrong autocomplete for className. I am currently trying out something like this in combination with glamor:
Your example is pretty much what I had in mind.
@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: