admin管理员组文章数量:1279043
I want to apply multiple classes to a ponent using a ternary operator. There is a shared ts theme file that includes the general button styles, but I would like to display their sizes in this particular ponent differently depending on screen width, so have added locally scoped classes for this too.
What is the best approach for this?
...tried this, not working:
...got it working using classNames.
I want to apply multiple classes to a ponent using a ternary operator. There is a shared ts theme file that includes the general button styles, but I would like to display their sizes in this particular ponent differently depending on screen width, so have added locally scoped classes for this too.
What is the best approach for this?
...tried this, not working:
...got it working using classNames.
Share Improve this question edited Sep 11, 2019 at 9:31 AKL012 asked Sep 11, 2019 at 8:57 AKL012AKL012 3996 silver badges14 bronze badges 3- So there is more than two classes to be applied to the same ponent? – minus.273 Commented Sep 11, 2019 at 9:10
-
as a string :) instead classes.saveButton `
${classes.saveButton} other-class
` – Kasia Commented Sep 11, 2019 at 9:17 -
Use module like
classnames
orclsx
. These are built for this purpose. – Sandip Nirmal Commented Sep 11, 2019 at 9:26
4 Answers
Reset to default 5I usually use a small npm package called classNames
: https://www.npmjs./package/classnames.
It exposes a function that takes a variable number of arguments. If you give strings, they will be basically joined with a space, so you have them all applied:
<Button className={ classNames('first', 'second') } ...
Will be like doing:
<Button className="first second" ...
A nice feature that this package has, is that it can take an object where the key is the class and the value is a boolean that indicates whether to add this class or not, something like:
<Button className={ classNames({ even: index % 2 === 0, disabled: props.isDisabled }) } ...
Now, if index
is something like 2
, the even
class is added to the element.
You can use clsx, I think it's already installed if you use material-ui, and you can see that in material-ui's documentation examples.
import clsx from 'clsx';
// Bellow examples are from the clsx npm site:
// Strings (variadic)
clsx('foo', true && 'bar', 'baz');
//=> 'foo bar baz'
// Objects
clsx({ foo:true, bar:false, baz:isTrue() });
//=> 'foo baz'
// Objects (variadic)
clsx({ foo:true }, { bar:false }, null, { '--foobar':'hello' });
//=> 'foo --foobar'
Material UI classes are piled to classname strings, you can chain them with a space between each one :
<Button
classes={{
root: isDesktop ? `${classes.saveButton} ${classes.secondStyle}` : classes.disabledSaveButton
}}
>
npm package classnames can do this for you in a cleaner way if you need to handle more plex cases.
- You can use two different styles files in css directory for Desktop and Mobile and in index file make switch to export right css. For example: Styles Directory that contains => 2 css files: Mobile,Desktop and in same directory index.js for export right one.
- By Using Theme Providers: styled-ponents
本文标签:
版权声明:本文标题:javascript - React.js question: How do I apply multiple classes to a component using a ternary operator? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741291525a2370577.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论