Performance: Tailwind with React components
This affects how fast the page loads and how quickly React components render with Tailwind styles applied.
const buttonClasses = "text-white bg-blue-500 hover:bg-blue-700 font-bold py-2 px-4 rounded";
function Button() {
return <button className={buttonClasses}>Click me</button>;
}function Button() {
return <button className="text-white bg-blue-500 hover:bg-blue-700 font-bold py-2 px-4 rounded">Click me</button>;
}| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Inline long Tailwind classes in JSX | Normal | Multiple on prop changes | Medium | [X] Bad |
| Reused Tailwind class strings | Normal | Single or minimal | Low | [OK] Good |
| Conditional inline class strings | Normal | Multiple on each render | Medium | [X] Bad |
| Separated static and dynamic classes | Normal | Minimal | Low | [OK] Good |
className attribute to add CSS classes to elements.className to style components quickly.className attribute with Tailwind utility classes -> Option CclassName, not class.className must be a string, so it should be inside quotes.function Card() {
return (
<div className="max-w-sm mx-auto bg-white rounded shadow-lg p-4">
<h2 className="text-xl font-bold mb-2">Hello</h2>
<p className="text-gray-700">Welcome to Tailwind!</p>
</div>
);
}max-w-sm (small max width), mx-auto (center horizontally), bg-white (white background), rounded (rounded corners), shadow-lg (large shadow), and p-4 (padding).text-xl (large text), font-bold (bold), and mb-2 (margin bottom). The paragraph uses text-gray-700 (medium gray text).function Button() {
return <button class="bg-green-500 text-white p-3">Submit</button>;
}className, not class.class instead of className in JSX -> Option AclassName value?<button className="?">Click</button>
md: to apply styles at medium screen sizes and above.bg-blue-500 for small screens and md:bg-green-500 to override on medium and larger.Pass the test and this pattern moves unknown