DEV Community

Cover image for Setting Up NativeWind in React Native
Dubjay18
Dubjay18

Posted on

3

Setting Up NativeWind in React Native

If you want to enhance your React Native Expo project with a powerful styling solution, NativeWind is an excellent choice. NativeWind allows you to use Tailwind CSS classes in your React Native components, making it easier to manage styles and maintain consistency across your application. This article will walk you through the steps to set up NativeWind in your React Native Expo project.

Table of Contents

  1. Introduction to NativeWind
  2. Prerequisites
  3. Setting Up a New Expo Project
  4. Installing NativeWind
  5. Configuring TailwindCSS
  6. Using NativeWind in Your Project
  7. Example Usage
  8. Conclusion

Introduction to NativeWind

NativeWind brings the utility-first CSS framework, Tailwind CSS, to the React Native world. Integrating NativeWind allows you to apply Tailwind-style classes directly to your React Native components, simplifying the styling process and making your codebase cleaner.

Prerequisites

Before you start, ensure you have the following installed on your system:

Node.js (version 12 or higher)
npm or yarn
Expo CLI

Setting Up a New Expo Project

If you don't already have an Expo project, you can create one by running:



npx create-expo-app MyNativeWindApp
cd MyNativeWindApp


Enter fullscreen mode Exit fullscreen mode

Installing NativeWind

To install NativeWind and its peer dependencies, run the following commands:



npm install nativewind && npm install -D tailwindcss@3.3.2


Enter fullscreen mode Exit fullscreen mode

Configuring TailwindCSS

Next, you need to configure TailwindCSS in your project. Create a tailwind.config.js file in the root of your project:



// tailwind.config.js
module.exports = {
+ content: ['./App.{js,jsx,ts,tsx}', './components/**/*.{js,jsx,ts,tsx}'],
  theme: {
    extend: {},
  },
  plugins: [],
}


Enter fullscreen mode Exit fullscreen mode

If you’re going to be writing Tailwind styles in other directories, be sure to include them in the content array.
Finally, add the Babel plugin for NativeWind to babel.config.js:



// babel.config.js
module.exports = function (api) {
  api.cache(true);
  return {
    presets: ["babel-preset-expo"],
+   plugins: ["nativewind/babel"],
  };
};


Enter fullscreen mode Exit fullscreen mode

Using NativeWind in Your Project

Now, you can start using NativeWind in your React Native components.



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

export default function App() {
    <SafeAreaView className={"flex-1 items-center justify-center bg-neutral-900"}>
    <StatusBar style="light" />
        <Text className="text-red-500 font-bold">They not like us</Text>
    </SafeAreaView>
}


Enter fullscreen mode Exit fullscreen mode

the visuals

Image description

Conclusion
Setting up NativeWind in your React Native project allows you to leverage the power of Tailwind CSS for styling your mobile application. By following the steps outlined in this guide, you can quickly integrate NativeWind and start using utility-first CSS classes in your React Native components.

SurveyJS custom survey software

Simplify data collection in your JS app with a fully integrated form management platform. Includes support for custom question types, skip logic, integrated CCS editor, PDF export, real-time analytics & more. Integrates with any backend system, giving you full control over your data and no user limits.

Learn more

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay