Replies: 2 comments 1 reply
-
Hey @shinokada! 👋 To Q1: Yes, your usage of To Q2: You don't need the most outer array, let trClass: string;
$: trClass = twMerge(
!getContext('noborder') && 'border-b last:border-b-0',
colors[color],
getContext('hoverable') && hoverColors[color],
getContext('striped') && stripColors[color],
$$props.class
); Using arrays makes sense when you have nested conditions. E.g. let's say we have this hypothetical scenario in which we want to apply some classes when twMerge(
'grid w-max gap-2',
forceHover && 'bg-gray-200'
!forceHover && 'bg-white',
!forceHover && !disabled && 'hover:bg-gray-200',
$$props.class,
) The code is a bit repetitive, it is harder to reason about and it's more likely that we create a bug, e.g. by not seeing that we put a We can simplify this code by replacing all these repeating conditions and use nesting with ternaries and arrays instead. twMerge(
'grid w-max gap-2',
forceHover
? 'bg-gray-200'
: ['bg-white', !disabled && 'hover:bg-gray-200'],
$$props.class
) Here it's clear that when |
Beta Was this translation helpful? Give feedback.
-
Thank you for your explanation. When I use your suggested code, I get the following on the line
|
Beta Was this translation helpful? Give feedback.
-
I have the following
twMerge
using[]
.Q1. Are these correct?
Q2. When do I need to use
[]
?$:
is a Svelte notation for reactivity.Beta Was this translation helpful? Give feedback.
All reactions