React Performance question #164
-
Hello, Thanks. |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
Hey @jdolinski1! 👋 TLDR: Yes, you can call Long version here 👇 I build tailwind-merge so you can use it like this within React components: function MyInput({ className, someBool, someOtherBool, ...props }) {
return (
<input
{...props}
className={twMerge(
'…',
someBool && '…',
someOtherBool ? '…' : '…',
someGlobalBool && '…',
className,
)}
/>
)
} The merging mechanism within This means that when Note that with input I don't mean the exact amount and types of arguments to twMerge('1 2 3 4 5') // runs merging logic
twMerge('1 2', '3 4 5') // returns cached result
twMerge('1 2', false && 'nope', '3', null, '4 5', '') // returns cached result
twMerge('1 2', true ? '3' : 'nope', ['4', true && '5', false && '6']) // returns cached result In the first production app I used tailwind-merge in, almost all design system components called I think function MyComponent(props) {
// ✅ This is fine
return <div className={twMerge('…', props.className)} />
// ❌ This is not necessary…
return <div className={twMerge('h-2 bg-red-500', props.hasBlueBg && 'bg-blue-500')} />
// ✅ …do this instead (in v1.7.0 and earlier, the function is called `join`)
return <div className={twJoin('h-2', props.hasBlueBg ? 'bg-blue-500' : 'bg-red-500')} />
// ❌ Please don't do this
return <div className={twMerge('h-2 bg-red-500')} />
} Hope this clears things up a little. Let me know if you want to go more into specifics somewhere. 😊 |
Beta Was this translation helpful? Give feedback.
Hey @jdolinski1! 👋
TLDR: Yes, you can call
twMerge
during render of many components without expecting performance issues. I built tailwind-merge exactly for this use case. But you should only call it when you actually need it because extreme usage might cause issues.Long version here 👇
I build tailwind-merge so you can use it like this within React components: