ReactHooks——class添加判断

mergeClass.ts

function mergeClass(...source:any[]){
    const classes:string[]=[];
    source.filter(Boolean).forEach((s)=>{
        if(!s){
            return;
        }
        if(typeof s==='string'){
            classes.push(s);
            return;
        }
        if(Array.isArray(s)){
            classes.push(mergeClass(...s));
            return;
        }
        if(typeof s ==="object"){
            Object.entries(s).forEach(([key,value])=>{
                if(!key||!value){
                    return
                }
                classes.push(String(key));
            })
            return;
        }
    })
    return classes.join(" ")
}
export default mergeClass;
export const classnames = mergeClass

使用

import mergeClass from '../../utils/mergeClass';//引入
      <div className={
        mergeClass({
          'list':true
        })
      }>

      </div>

你可能感兴趣的:(#,RCHooks,typescript,vue.js,react.js)