Day 06 - 那些~帮助开发的套件

如果有错误,欢迎留言指教~ Q_Q

classnames: 可以依照逻辑条件的将 className 动态是否显示

classnames() 可以传入N个 string 或 object,根据 component 的 state 或是 props 来改变状态,就可以串成想要显示的 className 们

基本用法

// 可以传入 string 或是 object
classNames('foo', { bar: true }); 
// => 'foo bar'

// 可以传入 array
// object 的 value 如果是 falsy 值,则不会显示
let arr = ['b', { c: true, d: false }];
classNames('a', arr); 
// => 'a b c'

// 也可以组合模板文字
let buttonType = 'primary';
classNames({ [`btn-${buttonType}`]: true });
// => 'btn-primary'

范例

import cx from 'classnames';

const App = ({ children }) => {
    const [isShowArrow, setIsShowArrow] = useState(false);

    return (
        <div className={cx('arrow', { 'is-show': isShowArrow })}
          {children}
        </div>
      );
}
import cx from 'classnames';

const App = ({ children }) => {
    const leftArrowClass = cx(
        'left-arrow',
        { hidden: !hasLeftArrow }
    );

    return (
        <div className={leftArrowClass}
          {children}
        </div>
      );
}

如果使用 css-modules,则将其 bind 使用

import classnames from 'classnames/bind';
import { tabHeaderStyle } from './Tab.style';

const cx = classnames.bind(tabHeaderStyle);

const App = ({ name }) => {
    return (
        <a className={cx('tab', {'isActive': true })}>
            {name}
        </a>
    );
}

idx: 帮助更方便的遍历 object 和 array 的属性

帮你简化需要判断过程中的值是否存在,如果中间属性为 null 或未定义,则 return

import idx from 'idx';

const news = idx(data, _ => _.data.items) || {};

const quotes: IAdAndNewProps[] = idx(data, _ => _.data.items.data);


<<:  Day6 - 变数的进阶操作

>>:  Day13-Go方法method

RISC-V on Rust 从零开始(4) - Rust 测试工具

这次要来谈的是Rust的测试框架,并且重新调整目录架构。 撰写测试 Rust本身就自带测试框架,无须...

伪类与伪元素-30天学会HTML+CSS,制作精美网站

昨天介绍了各种选择器,今天介绍伪类及伪元素样式设定,可以让画面有更多的样式变化,也减少html co...

Day 01-前言

自我介绍 这是我第一次参加铁人赛,内容可能很菜,希望大家多多包涵 动机 因为平常满常使用其他人制作的...

学习比较框架(learning comparative framework)

两个很棒的NIST准则: .NIST SP 800-16 .NIST SP 800-50 -IT安全...

Leetcode: 1971. Find if Path Exists in Graph

思路 用dps从start点走一遍,然後检查end点有没有finish。 程序码 class Sol...