- vừa được xem lúc

Hướng dẫn cài đặt tailwindCSS cho project react native

0 0 24

Người đăng: Trần Thanh Bảo

Theo Viblo Asia

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 tailwind-rn đ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:

npx setup-tailwind-rn

Sau khi chạy xong project sẽ tụ động xuất hiện 4 file:

file tailwind.css, taiwind.json 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 App:

import {TailwindProvider} from 'tailwind-rn';

import utilities from './tailwind.json';

Trường hợp này App đang là component root.

Cho App return Component TailwindProvider, tất cả các component còn lại là children của TailwindProvider. TailwindProvider nên bao bọc phía ngoài cùng tất cả, kể cả NavigationContainer, NativeBaseProvider, ...

const App = () => { return ( <TailwindProvider utilities={utilities} > {/* <MyComponent /> */} </TailwindProvider> );
};

File App.js sẽ trông như thế này:

import React from 'react';
import {TailwindProvider} from 'tailwind-rn';
import utilities from './tailwind.json'; const App = () => { return ( <TailwindProvider utilities={utilities} > {/* <MyComponent /> */} </TailwindProvider> );
};
export default App;

4.3: Sử dụng các class tailwind CSS

Tạo component MyComponent với nội dung như sau

import {useTailwind} from 'tailwind-rn'; // <===== const MyComponent = () => { const tailwind = useTailwind(); // <===== return ( <View style={tailwind('pt-12 items-center')}> <View style={tailwind('bg-blue-200 px-3 py-1 rounded-full')}> <Text style={tailwind('text-blue-800 font-semibold')}> Hello Tailwind </Text> </View> </View> );
};

5: run và debug ứng dụng

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:

yarn dev:tailwind

Cửa số thứ nhất chạy lệnh:

yarn android

hoặc

yarn ios

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ệ:

import React from 'react';
import {SafeAreaView, View, Text} from 'react-native'; import {TailwindProvider} from 'tailwind-rn';
import utilities from './tailwind.json'; const App = () => ( <TailwindProvider utilities={utilities}> <SafeAreaView style={tailwind('h-full')}> // <===== <View style={tailwind('pt-12 items-center')}> // <===== <View style={tailwind('bg-blue-200 px-3 py-1 rounded-full')}> // <===== <Text style={tailwind('text-blue-800 font-semibold')}> // <===== Hello Tailwind </Text> </View> </View> </SafeAreaView> </TailwindProvider>
); export default App; 

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

Nếu project thư mục có cấu trúc như này và muốn sử dụng class tailwind cho ProfileScreen, LoginScreen, HomeScreen, ...

projectname/
├─ src/
│ ├─ screens/
│ │ ├─ ProfileScreen.js
│ │ ├─ LoginScreen.js
│ │ ├─ HomeScreen.js
tailwind.config.js 

Trong file tailwind.config.js cần chỉ định path cho thuộc tính content'./src/screens/**.{js,jsx}' (mặc định content là mảng rỗng).

module.exports = { content: ['./src/screens/**.{js,jsx}'], theme: { extend: {}, }, plugins: [], corePlugins: require('tailwind-rn/unsupported-core-plugins'),
};

Bình luận

Bài viết tương tự

- vừa được xem lúc

Làm việc với Environment Variables trong React native.

1. Environment Variables là gì? Tại sao cần sử dụng Environment Variables. Đối với một sản phẩm ứng dụng thông thường sẽ được phát triển trên 3 môi trường:. .

0 0 52

- vừa được xem lúc

Tìm hiểu về kiến trúc của React Native

Nếu đã làm việc với mobile app thì chắc hẳn các bạn không còn xa lạ gì với các tên React Native. Nó là một open-source hay còn gọi là Cross-Platform do Facebook tạo ra cho phép các nhà phát triển có t

0 0 587

- vừa được xem lúc

Bài 1 - React Native DevOps các khái niệm và các cài đặt căn bản

Hướng dẫn setup jenkins agent để bắt đầu build mobile bằng jenkins cho devloper an tâm phát triển sản phẩm. . Tóm tắt. Các khái niệm.

0 0 9

- vừa được xem lúc

Bài 3 - Đăng ký Firebase và cấu hình Fastlane đẩy APK vs IPA lên Firebase Distribution (Phiên bản thử nghiệm)

Để tiện cho đội tester test sản phẩm và tự động hơn nên chúng ta cần làm CI - CD. . Tóm tắt. Các khái niệm.

0 0 11

- vừa được xem lúc

Kiến thức cơ bản về blockchain. Phát triển ứng dụng đầu tiên tạo ví blockchain với React Native và Web3.js

Lý thuyết cơ bản về block chain cho developer web front end. Blockchain là một công nghệ tiên tiến được sử dụng để lưu trữ và quản lý dữ liệu một cách an toàn và minh bạch.

0 0 14

- vừa được xem lúc

5 Compelling Reasons to Choose React Native for App Development

In today's digital world, having a powerful and user-friendly app is critical for the success of any business. With millions of apps available in the app stores, standing out from the competition can

0 0 7