How Install Tailwind CSS in a React Native TypeScript project
you can use Tailwind CSS in combination with the twrnc
library, which allows you to use Tailwind classes in React Native. Here’s a step-by-step guide to get you started
1) Initialize Your React Native Project:
If you haven’t already created your React Native project, you can do so using the React Native CLI or Expo.
For React Native CLI:
npx react-native init MyProject --template react-native-template-typescript
For Expo:
npx create-expo-app MyProject --template
cd MyProject
npx expo install expo-font
2) Install twrnc
:
Install twrnc
, a Tailwind CSS-like library for React Native.
npm install twrnc
Or using Yarn:
yarn add twrnc
3) Configure TypeScript:
Create a types/twrnc.d.ts
file to include type definitions for twrnc
.
// types/twrnc.d.ts
declare module 'twrnc' {
const tw: (classNames: TemplateStringsArray | string) => import('react-native').ViewStyle;
export default tw;
}
Make sure to include the types
folder in your tsconfig.json
.
{
"compilerOptions": {
"typeRoots": ["./node_modules/@types", "./types"]
}
}
4) Use Tailwind Classes in Your Components:
You can now use Tailwind CSS classes in your React Native components.
// App.tsx
import React from 'react';
import { View, Text } from 'react-native';
import tw from 'twrnc';
const App = () => {
return (
<View style={tw`flex-1 justify-center items-center bg-blue-500`}>
<Text style={tw`text-white text-lg`}>Hello, Tailwind!</Text>
</View>
);
};
export default App;
5) Customizing Tailwind Configuration (Optional):
You can customize your Tailwind CSS configuration by creating a tailwind.config.js
file. However, twrnc
uses its own default Tailwind configuration, and customization options may be limited compared to the full Tailwind CSS.
6) Running Your Project:
Run your React Native project to see the changes.
For React Native CLI:
npx react-native run-android
npx react-native run-ios
For Expo:
npx expo start
I believe, this would be enough for getting started with Tailwind CSS in React Native. If you want to explore more, you can visit NativeWind
and experiment.