凌乱的类名构造 [英] messy classnames construction
本文介绍了凌乱的类名构造的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
任何人都可以提出一种方法来清理这种凌乱的类名构造:
Can anyone suggest a way to clean up this messy classname construction:
const ButtonTemplate = props => {
const themed = `btn-${props.theme}`
const themedButton = `${styles[themed]} ${themed}${(props.disabled) ? ' disabled' : ''}}`
return (
<button className={`${styles.btn} ${themedButton}`} type='button' onClick={props.onClick}>{props.children}</button>
)
}
推荐答案
那
function ButtonTemplate({theme, disabled, onClick, children}) {
const themed = `btn-${theme}`;
return (
<button className={[
styles.btn,
styles[themed],
themed,
disabled ? 'disabled' : ''
].join(" ")} type='button' onClick={onClick}>{children}</button>
);
}
这篇关于凌乱的类名构造的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文