Hướng dẫn tailwind css react - tailwind css phản ứng

Hướng dẫn tailwind css react - tailwind css phản ứng

Đã đă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:https://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

Hướng dẫn tailwind css react - tailwind css phản ứng

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
0

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.
Hướng dẫn tailwind css react - tailwind css phản ứng

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.
Hướng dẫn tailwind css react - tailwind css phản ứng

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

npm install -D tailwindcss postcss autoprefixer
5:

import {TailwindProvider} from 'tailwind-rn';

npm install -D tailwindcss postcss autoprefixer
5 đang là component root.

import utilities from './tailwind.json';

cd my-project
1

Trường hợp này

npm install -D tailwindcss postcss autoprefixer
5 đang là component root.

cd my-project
2

Cho 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, ...

File

npx tailwindcss init -p
3 sẽ trông như thế này:

cd my-project
3

4.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
4

5: run và debug ứng dụng

cd my-project
5

hoặc

cd my-project
6

Mở đồ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
8

2. Config đường dẫn các component con

cd my-project
9

nế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
0

hoặc

npm install -D tailwindcss postcss autoprefixer
1