Published on

Fixing TS Error with 'className' in React Native + Tailwind

Authors
  • Name
    Ripal & Zalak
    Twitter

Fixing TS Error with 'className' in React Native + Tailwind

When working with React Native, TypeScript, and Tailwind CSS, you may encounter the following TypeScript error:

No overload matches this call.
Type '{ children: Element; className: string; }' is not assignable to type 'IntrinsicAttributes & IntrinsicClassAttributes<View> & Readonly<ViewProps>'.
Property 'className' does not exist on type 'ViewProps'.

This error occurs because the className prop is not natively supported by React Native's components (like View, Text, etc.). However, when using libraries like NativeWind (Tailwind CSS for React Native), you can enable the className prop by following these steps:

Step 1: Install NativeWind

First, ensure you have NativeWind installed:

npm install nativewind

If you're using Expo, add it as a development dependency:

expo install nativewind

Step 2: Update TypeScript Configuration

To make TypeScript recognize the className prop, you need to reference NativeWind's type definitions.

Option 1: Create a global.d.ts File

  1. Create a new file named global.d.ts at the root level of your project.
  2. Add the following line:
/// <reference types="nativewind/types" />

Option 2: Use app.d.ts or nativewind-env.d.ts

Alternatively, you can create a specific type declaration file (e.g., app.d.ts) and include:

/// <reference types="nativewind/types" />

Both approaches achieve the same goal by informing TypeScript about the className prop added by NativeWind.

Step 3: Verify tsconfig.json

Ensure your tsconfig.json extends the Expo TypeScript configuration:

{
  "extends": "expo/tsconfig.base",
  "compilerOptions": {
    "strict": true
  }
}

Step 4: Use className in Components

With the above setup complete, you can now use the className prop without any TypeScript errors. Here's an example:

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

export default function Navigation() {
  return (
    <View className="grid h-screen w-screen place-items-center">
      <Text className="text-red-800">Test</Text>
    </View>
  )
}

Additional Debugging Tips

If the issue persists after following the steps:

  1. Clear Cache: Run the following command to clear the cache and restart Metro Bundler:

    npx expo start --clear
    
  2. Ensure NativeWind is Installed Correctly: Verify that the nativewind package is installed by checking your node_modules directory.

  3. Use Correct File Structure: Ensure your TypeScript declaration file (e.g., global.d.ts) is placed in the correct directory (typically the root of your project).

  4. Update Dependencies: Make sure you're using the latest version of NativeWind and TypeScript:

    npm update nativewind typescript
    

FAQ

Why does this error occur in React Native?

React Native components like View and Text do not natively support the className prop. Libraries like NativeWind extend these components to enable className for Tailwind CSS styling.

What if I still see errors after following these steps?

Double-check your TypeScript configuration and ensure you've created the global.d.ts file with the correct reference.

Can I disable TypeScript errors temporarily?

While not recommended, you can suppress errors by adding // @ts-nocheck at the top of the file. However, this should be a last resort as it disables TypeScript checking for the entire file.

Is NativeWind the only library that adds className support?

No, other libraries like react-native-tailwindcss also enable Tailwind CSS usage in React Native. However, NativeWind is widely used and actively maintained.