it's time to merge className in react
See original GitHub issuewell, react is very good, i like it,but i have some question, and it’s time to fixed. 1.why support style object
style={color: 'white',backgroundImage: 'url(' + imgUrl + ')'}
but not support className, i think className should support string,array,object,there has a classname repertory.i just want to write code like this:
<div className={['button','icon-red']}></div>
<div className={{opened:true,cloth:'red'}}></div>
<div className={'button',{opened:true,cloth:'red'}}></div>
//not
var className = react.addons.className;
var someVar = className({ //that is puzzled i think
opened:true,
cloth:'red'
});
<div className={someVar}>
or will it be support in the feture?
Issue Analytics
- State:
- Created 8 years ago
- Comments:7 (4 by maintainers)
Top Results From Across the Web
How to add multiple classes to a ReactJS Component?
I use classnames when there is a fair amount of logic required for deciding the classes to (not) use. An overly simple example:...
Read more >Multiple CSS classes in React - Programming with Mosh
If you just want to apply multiple classes to some element and this class set never changes, there's no need to complicate things:...
Read more >Using Multiple Class Names in React | Delft Stack
Applying multiple classes to JSX elements works a little differently than HTML. Let's take a look at how it works.
Read more >How to use the merge-class-names function in merge ... - Snyk
A function that merges given class names, no matter their format: string with single or multiple class names or an array of class...
Read more >How to add multiple classes to className using styles - Reddit
But an array of the classnames with a join would work fine if you kept ... If you haven't worked with TypeScript yet...
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
Using classnames (which is now a dedicated module that can be used outside of React) is the best option. Even switching to classList doesn’t cover all the use cases that classnames covers. And @zpao has already partially covered the fact that props passed to ReactDOM elements are based on their DOM property equivalents not their HTML properties (
el.style
notstyle=""
,el.htmlFor
notfor=""
,el.acceptCharset
notaccept-charset=""
,el.useMap
notusemap=""
).The initial examples also aren’t very good ideas for being supported directly by react:
This makes sense in classnames
className={cx('button', {opened:true})}
but it doesn’t map to a valid js value that can be passed to props.I’m also not sure what this example is supposed to be requesting.
cloth: 'red'
doesn’t make sense. String keys aren’t even a pattern implemented in classnames.Thanks for bringing it up. It’s a valid question and one we’ve discussed before. For now, our current behavior exists to stay inline with expected DOM behavior.
className
is a string. If we did make a change like suggested, we would probably just useclassList
, which is another DOM property more like an array. We’d still need to convert from a vanilla array and make add/remove calls (we also then need to do more work to make sure we clone the array you provide as props - simple equality will no longer work since it would be very possible to pass the same array reference as props on 2 render passes)Regardless, this is really only important at the DOM element level. You composite components can take whatever props they want and convert to strings when rendering DOM elements.