Đã đăng vào thg 9 15, 8:36 SA 1 phút đọc 1 phút đọc
Tailwind CSS và React JS
Tôi thấy Tailwind CSS là 1 thư viện CSS rất được ưa chuộng hiện nay. Với Bootstrap thì khả năng tùy biến thấp hơn nhiều so với Tailwind CSS. Tuy phải nhớ nhiều hơn vì mỗi class của Tailwind sẽ tương ứng với 1 thuộc tính trong CSS.
1. Tạo 1 project React
npx create-react-app my-project
- Đi đến thư mục của dự án
cd my-project
- Lưu ý: bạn phải cài đặt Node JS và npm, npx. Bạn có thể tham khảo link này://docs.npmjs.com/downloading-and-installing-node-js-and-npm
2. Cài đặt Tailwind CSS
npm install -D tailwindcss postcss autoprefixer
npx tailwindcss init -p
3. Thêm thông tin vào file tailwind.config.js
file tailwind.config.js
/** @type {import['tailwindcss'].Config} */
module.exports = {
content: [
"./src/**/*.{js,jsx,ts,tsx}",
],
theme: {
extend: {},
},
plugins: [],
}
4. Thêm các file của thư viện Tailwin vào file css của dự án
Ví dụ tôi sẽ thêm vào file index.css của React js. Bạn có thể thêm vào các file khác của dự án sau đó add file này vào file gốc index.html
index.css
@tailwind base;
@tailwind components;
@tailwind utilities;
5. Chạy thử dự án
App.js
export default function App[] {
return [
Hello world!
]
}
Thêm code bên trên vào file App.js và chạy thử. Bạn có thể thấy các kết quả của các class tử tailwind như text-3xl, font-bold
All rights reserved
Bước 1: Int project react native [LT;DR]
cài đặt project react native
yarn add global react-native-cli
yarn add global react-native
react-native init projectname
ngoài ra các bạn có thể tạo project bằng expo
cd projectname
Bước 2: Add dependency
Chạy lệnh:
yarn add tailwind-rn
Tại thời điểm viết bài thư viện
npm install -D tailwindcss postcss autoprefixer
2 đang ở version 4.2.0, có 3.5k start trên gitHub, 6000 lượt download mỗi tuần trên npm.Bước 3: Generate các file config
Chạy lệnh:
cd my-project
0Tại thời điểm viết bài thư viện
npm install -D tailwindcss postcss autoprefixer
2 đang ở version 4.2.0, có 3.5k start trên gitHub, 6000 lượt download mỗi tuần trên npm.Bước 3: Generate các file config
Sau khi chạy xong project sẽ tụ động xuất hiện 4 file:
file
npm install -D tailwindcss postcss autoprefixer
3, npm install -D tailwindcss postcss autoprefixer
4 là sản phẩm cuối dùng được tự động build ra.Bước 4: setup các component
Tiếp tục làm theo hướng dẫn
4.1: setup Component root
import 2 dòng này vào Component
5:npm install -D tailwindcss postcss autoprefixer
import {TailwindProvider} from 'tailwind-rn';
5 đang là component root.npm install -D tailwindcss postcss autoprefixer
import utilities from './tailwind.json';
cd my-project
1Trường hợp này
npm install -D tailwindcss postcss autoprefixer
5 đang là component root.cd my-project
2Cho npm install -D tailwindcss postcss autoprefixer
5 return Component npm install -D tailwindcss postcss autoprefixer
8, tất cả các component còn lại là children của npm install -D tailwindcss postcss autoprefixer
8. npm install -D tailwindcss postcss autoprefixer
8 nên bao bọc phía ngoài cùng tất cả, kể cả npx tailwindcss init -p
1, npx tailwindcss init -p
2, ...
npm install -D tailwindcss postcss autoprefixer
npm install -D tailwindcss postcss autoprefixer
npm install -D tailwindcss postcss autoprefixer
npm install -D tailwindcss postcss autoprefixer
npx tailwindcss init -p
npx tailwindcss init -p
File
npx tailwindcss init -p
3 sẽ trông như thế này:cd my-project
34.3: Sử dụng các class tailwind CSS
Tạo component
npx tailwindcss init -p
4 với nội dung như sauđồng thời 2 của số terminal mỗi khi muốn run app trên điện thoại hoặc máy ảo:5: run và debug ứng dụng
cd my-project
45: run và debug ứng dụng
cd my-project
5hoặc
cd my-project
6Mở đồng thời 2 của số terminal mỗi khi muốn run app trên điện thoại hoặc máy ảo:
Cửa số thứ nhất chạy lệnh:
NHỮNG LƯU Ý KHÔNG CÓ TRONG DOCS
1. Class tailwind không có tác dụng khi sử dụng trực tiếp bên trong component root.Trường hợp dưới đây là không hợp lệ:
cd my-project
7 cd my-project
82. Config đường dẫn các component con
cd my-project
9nếu có nhiều thư mục với các đường dẫn khác nhau thì tạo thành mảng các đường dẫn:
npm install -D tailwindcss postcss autoprefixer
0hoặc
npm install -D tailwindcss postcss autoprefixer
1