DEV Community

SWAPNIL AHMMED SHISHIR
SWAPNIL AHMMED SHISHIR

Posted on

Tailwind CSS to your React Native projects!

It's awesome you're looking to bring the power of Tailwind CSS to your React Native projects! While Tailwind itself is designed for the web, we can achieve a very similar workflow using a library called NativeWind.

Here's a step-by-step guide to get you set up:

Step 1: Set up your React Native project

  • If you already have a React Native project, you can skip this step.
  • If you're starting fresh, create a new React Native project using your preferred method (React Native CLI or Expo).

    • React Native CLI:

      npx react-native init MyAwesomeApp
      cd MyAwesomeApp
      
    • Expo:

      npx create-expo-app MyAwesomeApp
      cd MyAwesomeApp
      

Step 2: Install NativeWind and Tailwind CSS

Now, let's add the necessary packages:

npm install nativewind tailwindcss
Enter fullscreen mode Exit fullscreen mode
  • nativewind: This library bridges Tailwind's utility-first approach to React Native.
  • tailwindcss: We still need the core Tailwind CSS library for NativeWind to work.

Step 3: (Important) Install react-native-reanimated

NativeWind relies on react-native-reanimated, so make sure to install it:

npm install react-native-reanimated
Enter fullscreen mode Exit fullscreen mode

Step 4: Set up Tailwind CSS configuration

Generate a tailwind.config.js file:

npx tailwindcss init
Enter fullscreen mode Exit fullscreen mode

This creates a basic Tailwind configuration file in your project root. You'll likely want to customize this later, but the default is a good starting point.

Step 5: Configure babel.config.js

Open your babel.config.js and add the NativeWind Babel plugin:

module.exports = {
  presets: ['module:metro-react-native-babel-preset'],
  plugins: [
    'react-native-wind/babel',
    // If you're using React Native Reanimated, make sure it's LAST:
    // 'react-native-reanimated/plugin',
  ],
};
Enter fullscreen mode Exit fullscreen mode
  • Crucial: Ensure that 'react-native-wind/babel' is in your plugins array.
  • Reanimated: If you use React Native Reanimated, the Reanimated plugin must be the last one in the plugins array.

Step 6: (Crucial) Configure metro.config.js

You need to tell React Native's bundler (Metro) how to handle NativeWind. Create or update your metro.config.js:

const { getDefaultConfig } = require('metro-config');

module.exports = (async () => {
  const {
    resolver: { sourceExts, assetExts },
  } = await getDefaultConfig();
  return {
    transformer: {
      babelTransformerPath: require.resolve('react-native-wind/babel'),
    },
    resolver: {
      assetExts: assetExts.filter((ext) => ext !== 'svg'), // Important: Exclude svg
      sourceExts: [...sourceExts, 'svg'],
    },
  };
})();
Enter fullscreen mode Exit fullscreen mode
  • babelTransformerPath: This tells Metro to use NativeWind's Babel transformer.
  • assetExts and sourceExts: This part is essential, especially if you use SVGs. It prevents conflicts and ensures Metro handles both correctly.

Step 7: Start using Tailwind-like classes!

Now you can style your React Native components with Tailwind-like syntax, using the tw function.

import React from 'react';
import { View, Text } from 'react-native';
import { tw } from 'nativewind';

const MyComponent = () => {
  return (
    <View style={tw`bg-blue-500 p-4 rounded-md shadow-lg`}>
      <Text style={tw`text-white text-lg font-bold`}>
        Hello, NativeWind!
      </Text>
      <Text style={tw`text-blue-200 text-sm mt-2`}>
        Styled with NativeWind
      </Text>
    </View>
  );
};

export default MyComponent;
Enter fullscreen mode Exit fullscreen mode
  • import { tw } from 'nativewind';: Import the tw function.
  • `style={tw\...`}`: Use backticks () and thetw` tag to write your Tailwind classes.

Step 8: Run your app

You'll probably want to clear your Metro cache to ensure the changes are applied:

`bash
npx react-native start --reset-cache

or

yarn start --reset-cache
`

Then, run your app on your device or emulator as usual:

`bash
npx react-native run-ios # or run-android

or

yarn ios #or android
`

Key things to remember

  • react-native-reanimated: NativeWind depends on this.
  • metro.config.js: Configuring Metro correctly is crucial for NativeWind to process the styles.
  • Babel plugin order: If using Reanimated, make sure its plugin is last in your babel.config.js.
  • tw function: Use this to apply styles in your components.

That's it! You've successfully set up NativeWind to use Tailwind-like styling in your React Native application.

Top comments (0)