Skip to content

Commit cfc4a15

Browse files
committed
update best-practices-for-react-development-with-tailwind-css
1 parent d13e8e6 commit cfc4a15

File tree

2 files changed

+46
-51
lines changed
  • content/articles/best-practices-for-react-development-with-tailwind-css

2 files changed

+46
-51
lines changed

content/articles/best-practices-for-react-development-with-tailwind-css/index.md

+45-50
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,15 @@ author: Pentakota Avilash
66
lastmod: 2024-05-16T13:12:12-07:00
77
tags:
88
- react
9-
- tailwind
9+
- tailwind-css
1010
draft: false
1111
---
1212

1313
**Authors**:
1414

1515
- [Kulsoom Nisha](/authors/kulsoom-nisha/)
1616
- [Pentakota Avilash](/authors/pentakota-avilash/)
17+
- [Geeth Sowri](/authors/geeth-sowri/)
1718

1819
---
1920

@@ -82,68 +83,69 @@ React and Tailwind CSS have become highly popular in the web development communi
8283
- **PurgeCSS**: Enable PurgeCSS in your Tailwind configuration to remove unused CSS in production, reducing file size.
8384
- **JIT Mode**: Use Just-in-Time (JIT) mode for faster build times and on-demand generation of styles.
8485

85-
### Integrating React with Tailwind CSS
86+
## Integrating React with Tailwind CSS
8687

87-
#### Setup
88+
### Setup
8889

89-
1. **Install Tailwind CSS** using npm or yarn.
90+
1. Install Tailwind CSS using npm or yarn.
9091

91-
```bash
92-
npm install tailwindcss
93-
npx tailwindcss init
94-
```
92+
```sh
93+
npm install tailwindcss
94+
npx tailwindcss init
95+
```
9596

96-
2. **Configuration**:
97-
Configure `tailwind.config.js` and `postcss.config.js` to include Tailwind’s directives. Import Tailwind styles in your main CSS file.
97+
2. Configuration:
98+
Configure tailwind.config.js and postcss.config.js to include Tailwind’s directives.
99+
Import Tailwind styles in your main CSS file.
98100

99-
```css
100-
@tailwind base;
101-
@tailwind components;
102-
@tailwind utilities;
103-
```
104-
105-
#### Usage in Components
101+
```css
102+
@tailwind base;
103+
@tailwind components;
104+
@tailwind utilities;
105+
```
106106

107+
3. Usage in Components:
107108
Apply Tailwind’s utility classes directly in React components.
108109

109-
```javascript
110-
import React from 'react';
110+
```javascript
111111

112-
const Button = () => (
113-
<button className="bg-blue-500 text-white font-bold py-2 px-4 rounded">
114-
Click Me
115-
</button>
116-
);
112+
import React from 'react';
117113

118-
export default Button;
119-
```
114+
const Button = () => (
115+
<button className="bg-blue-500 text-white font-bold py-2 px-4 rounded">
116+
Click Me
117+
</button>
118+
);
120119

121-
#### Custom Components
120+
export default Button;
121+
```
122+
123+
## Custom Components
122124

123125
Create reusable components with Tailwind CSS.
124126

125127
```javascript
128+
126129
const Card = ({ title, description }) => (
127130
<div className="max-w-sm rounded overflow-hidden shadow-lg">
128131
<div className="px-6 py-4">
129132
<div className="font-bold text-xl mb-2">{title}</div>
130-
<p className="text-gray-700 text-base">
131-
{description}
132-
</p>
133+
<p className="text-gray-700 text-base">{description}</p>
133134
</div>
134135
</div>
135136
);
136137

137138
export default Card;
138139
```
139140

140-
#### More Examples of React Components Styled with Tailwind CSS
141+
Here are more examples of React components styled with Tailwind CSS. These examples cover various common UI elements like buttons, forms, modals, and navigation bars.
141142

142-
##### Example 1: Button Component
143+
1. Example 1: Button Component
143144

144145
A simple button component with different styles for primary and secondary buttons.
145146

146147
```javascript
148+
147149
import React from 'react';
148150

149151
const Button = ({ type = 'primary', children, onClick }) => {
@@ -163,11 +165,12 @@ const Button = ({ type = 'primary', children, onClick }) => {
163165
export default Button;
164166
```
165167

166-
##### Example 2: Form Component
168+
2. Example 2: Form Component
167169

168170
A form component with input fields styled using Tailwind CSS.
169171

170172
```javascript
173+
171174
import React, { useState } from 'react';
172175

173176
const Form = () => {
@@ -220,7 +223,7 @@ const Form = () => {
220223
export default Form;
221224
```
222225

223-
##### Example 3: Modal Component
226+
3. Example 3: Modal Component
224227

225228
A modal component that can be toggled open and closed.
226229

@@ -264,11 +267,12 @@ const App = () => {
264267
export default App;
265268
```
266269

267-
##### Example 4: Navigation Bar Component
270+
4. Example 4: Navigation Bar Component
268271

269272
A responsive navigation bar component.
270273

271274
```javascript
275+
272276
import React from 'react';
273277

274278
const NavBar = () => {
@@ -277,18 +281,10 @@ const NavBar = () => {
277281
<div className="container mx-auto flex justify-between items-center">
278282
<div className="text-white text-lg font-semibold">MyApp</div>
279283
<div className="hidden md:flex space-x-4">
280-
<a href="#home" className="text-gray-300 hover:text-white">
281-
Home
282-
</a>
283-
<a href="#about" className="text-gray-300 hover:text-white">
284-
About
285-
</a>
286-
<a href="#contact" className="text-gray-300 hover:text-white">
287-
Contact
288-
</a>
289-
290-
291-
</div>
284+
<a href="#home" className="text-gray-300 hover:text-white">Home</a>
285+
<a href="#about" className="text-gray-300 hover:text-white">About</a>
286+
<a href="#contact" className="text-gray-300 hover:text-white">Contact</a>
287+
</div>
292288
</div>
293289
</nav>
294290
);
@@ -297,11 +293,12 @@ const NavBar = () => {
297293
export default NavBar;
298294
```
299295

300-
##### Example 5: Card Component
296+
1. Example 5: Card Component
301297

302298
A card component for displaying content in a structured format.
303299

304300
```javascript
301+
305302
import React from 'react';
306303

307304
const Card = ({ title, description }) => {
@@ -314,8 +311,6 @@ const Card = ({ title, description }) => {
314311
</div>
315312
);
316313
};
317-
318-
export default Card;
319314
```
320315

321316
### Component-Driven Development

public

0 commit comments

Comments
 (0)