React Native Unistyles

3.2.3 · active · verified Tue Apr 21

react-native-unistyles is a high-performance, universal styling solution for React Native, designed to enhance developer experience and application performance across iOS, Android, Web, and Expo environments. It leverages a shared C++ core and JSI bindings, powered by Nitro Modules, to achieve styling without triggering unnecessary re-renders in the React tree. The library is currently stable at version 3.2.3, with a rapid release cadence addressing features and bug fixes. Key differentiators include its unique custom web parser supporting CSS classes and pseudo-classes, tight integration with React Native's Fabric and Shadow Tree for optimal rendering, and significant performance benefits, adding under 0.1ms to StyleSheet processing. It allows developers to share nearly 100% of styles across platforms in a monorepo setup, register multiple themes, and dynamically switch them with a single function call, all without introducing new components into the view hierarchy. This approach ensures a clean and efficient component tree.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to set up Unistyles by defining multiple themes and responsive breakpoints. It shows how to register them with `UnistylesRegistry`, create a stylesheet using `createStyleSheet`, apply styles to components using `useStyles`, and dynamically toggle between themes at runtime using `UnistylesRuntime`. This setup provides type-safe, performant, and responsive styling for React Native applications.

import React from 'react';
import { View, Text, Pressable } from 'react-native';
import {
  UnistylesRegistry,
  createStyleSheet,
  useStyles,
  UnistylesRuntime
} from 'react-native-unistyles';

// 1. Define your themes
const lightTheme = {
  colors: {
    background: '#FFFFFF',
    text: '#000000',
    primary: '#6200EE'
  }
} as const; // 'as const' helps with type inference

const darkTheme = {
  colors: {
    background: '#121212',
    text: '#FFFFFF',
    primary: '#BB86FC'
  }
} as const;

// 2. Define your breakpoints (optional, but powerful)
const breakpoints = {
  sm: 0,
  md: 768,
  lg: 1024
} as const;

// 3. Register themes and breakpoints - crucial for TypeScript type inference
type AppBreakpoints = typeof breakpoints;
type AppThemes = {
  light: typeof lightTheme;
  dark: typeof darkTheme;
};

declare module 'react-native-unistyles' {
  export interface UnistylesBreakpoints extends AppBreakpoints {}
  export interface UnistylesThemes extends AppThemes {}
}

UnistylesRegistry.addBreakpoints(breakpoints)
  .addThemes({
    light: lightTheme,
    dark: darkTheme
  })
  .setInitialTheme('light'); // Set initial theme

// 4. Create your stylesheet
const stylesheet = createStyleSheet((theme) => ({
  container: {
    flex: 1,
    justifyContent: 'center',
    alignItems: 'center',
    backgroundColor: theme.colors.background
  },
  text: {
    color: theme.colors.text,
    fontSize: 20,
    marginBottom: 20,
    // Responsive styling using breakpoints
    $$container: {
      lg: { fontSize: 30 },
      md: { fontSize: 24 }
    }
  },
  button: {
    backgroundColor: theme.colors.primary,
    paddingVertical: 12,
    paddingHorizontal: 24,
    borderRadius: 8
  },
  buttonText: {
    color: theme.colors.text === '#000000' ? '#FFFFFF' : '#000000', // Ensure contrast
    fontSize: 16,
    fontWeight: 'bold'
  }
}));

// 5. Use the stylesheet in your component
export default function App() {
  const { styles, theme } = useStyles(stylesheet);

  const toggleTheme = () => {
    UnistylesRuntime.setTheme(theme.name === 'light' ? 'dark' : 'light');
  };

  return (
    <View style={styles.container}>
      <Text style={styles.text}>
        Current Theme: {theme.name}
      </Text>
      <Pressable onPress={toggleTheme} style={styles.button}>
        <Text style={styles.buttonText}>Toggle Theme</Text>
      </Pressable>
    </View>
  );
}

view raw JSON →