What is the least performance impacting approach to wrapping lines in react className attr when using tailwind css?

huangapple go评论137阅读模式
英文:

What is the least performance impacting approach to wrapping lines in react className attr when using tailwind css?

问题

Example A:

export function ExampleA() {
    return (
        <button className={
            'border border-solid border-[#00000026] ' +
            'px-5 py-3 rounded hover:bg-black/10 ' +
            'transition-colors duration-300 ease-out'
        }>
            Sample
        </button>
    );
}

Example B:

export function ExampleB() {
    return (
        <button className={[
            'border border-solid border-[#00000026]',
            'px-5 py-3 rounded hover:bg-black/10',
            'transition-colors duration-300 ease-out'
        ].join(' ')}>
            Sample
        </button>
    );
}

Example C:

import clsx from 'clsx'

export function ExampleC() {
    return (
        <button className={clsx(
            'border border-solid border-[#00000026]',
            'px-5 py-3 rounded hover:bg-black/10',
            'transition-colors duration-300 ease-out',
        )}>
            Sample
        </button>
    );
}
英文:

What is the better way to break lines in the className attribute when using tailwind css?

Based on the examples below, which one will impact performance the least?

If I use an array with the classes and then use the array join method as in example B, will react call the join method on every render, or will the typescript transpile it to a static string as it doesn't contain any dynamic values?

Example A:

export function ExampleA() {
    return (
        <button className={
            'border border-solid border-[#00000026] ' +
            'px-5 py-3 rounded hover:bg-black/10 ' +
            'transition-colors duration-300 ease-out'
        }>
            Sample
        </button>
    );
}

Example B:

export function ExampleB() {
    return (
        <button className={[
            'border border-solid border-[#00000026]',
            'px-5 py-3 rounded hover:bg-black/10',
            'transition-colors duration-300 ease-out'
        ].join(' ')}>
            Sample
        </button>
    );
}

Example C:

import clsx from 'clsx';

export function ExampleC() {
    return (
        <button className={clsx(
            'border border-solid border-[#00000026]',
            'px-5 py-3 rounded hover:bg-black/10',
            'transition-colors duration-300 ease-out',
        )}>
            Sample
        </button>
    );
}

答案1

得分: 1

Ideally you want to let tailwind set the order, because they do matter.

It is an annoyance for sure to have a super long line. But not a big deal imo.

That said, I would go with C or A. A will default to a single string at compilation and clsx will do its thing as well.

But the B will always do a concat in an array, which is not a big deal in general but should be definitely avoided not only cause it creates an array just to join it, but it also looks terrible.

英文:

Ideally you want to let tailwind set the order, because they do matter.

It is an annoyance for sure to have a super long line. But not a big deal imo.

That said, I would go with C or A. A will default to a single string at compilation and clsx will do its thing as well.

But the B will always do a concat in an array, which is not big deal in general but should be definitely avoided not only cause it creates and array just to join it. but it also look terrible.

答案2

得分: 1

根据我看,最高效的解决方案可能是使用包含类的数组,然后使用join()函数将它们连接起来(如示例B中所示)。这是因为示例A的字符串连接可能会导致在每次渲染时进行无意义的字符串分配,这可能会对性能产生不利影响,特别是如果组件经常显示。

join()方法不会被TypeScript编译成静态字符串,以响应您的查询。每次渲染都将使用join()函数,因为数组是动态的,根据组件的属性或状态而变化,而不是静态字符串。

使用实用程序库,比如clsx(如示例C中所示),也可以是一个高性能的选项,因为它处理合并多个类名并消除重复。这与示例B类似。不过,额外的函数调用可能会导致一些额外的开销。

总之,大多数情况下,这些不同技巧的性能影响可能是微不足道的。选择一个易于阅读和维护的方法更为重要。

英文:

Well it is opiniated question, accroding to me the most efficient solution is probably to use an array containing the classes and then link them using the join() function (as in example B). This is due to the possibility that example A's string concatenation may result in pointless string allocations on each render, which may have an adverse effect on performance if the component is displayed frequently.

And the join() method will not be transpiled by TypeScript into a static string, in response to your query. Every render will use the join() function as the array is dynamic and subject to changes based on component props or state rather than being a static string.

Using a utility library, such as clsx (as in example C), can also be a performant option, since it handles merging several class names and gets rid of any duplication. This is similar to example B. Yet, the additional function call may result in some additional cost.

To summarize, most of the time, the performance impact of these various techniques will probably be insignificant. The selection of an approach that is simple to read and keep is more crucial.

huangapple
  • 本文由 发表于 2023年3月4日 05:55:50
  • 转载请务必保留本文链接:https://go.coder-hub.com/75632198.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定