How to Add Linear Gradient in React Native - Complete Guide
Mahesh Waghmare Linear gradients are a powerful way to add visual depth and modern aesthetics to your React Native applications. Whether you’re creating beautiful button designs, card backgrounds, or full-screen gradients, understanding how to implement gradients effectively can significantly enhance your app’s user interface.
This comprehensive guide covers everything you need to know about adding linear gradients in React Native, from basic setup to advanced techniques and performance optimization.
Understanding Linear Gradients
A linear gradient is a gradual transition between two or more colors along a straight line. In React Native, gradients are not natively supported, so we use the react-native-linear-gradient library, which provides a cross-platform solution for both iOS and Android.
Key Concepts:
- Colors: The colors that transition in the gradient
- Direction: The angle or direction of the gradient (horizontal, vertical, diagonal)
- Locations: Where each color starts/stops in the gradient (0.0 to 1.0)
- Start/End Points: Specific coordinates where gradient begins and ends
Common Use Cases:
- Button backgrounds
- Card headers
- App backgrounds
- Overlays on images
- Status bars
- Navigation headers
Installation and Setup
Installing react-native-linear-gradient
Using npm:
npm install react-native-linear-gradient
Using yarn:
yarn add react-native-linear-gradient
Platform-Specific Setup
iOS (CocoaPods):
cd ios && pod install && cd ..
Android: No additional setup required for React Native 0.60+
Expo: If using Expo, you may need to use expo-linear-gradient instead:
expo install expo-linear-gradient
Import the Component
import LinearGradient from 'react-native-linear-gradient';
Basic Gradient Implementation
Simple Two-Color Gradient
The most basic gradient uses two colors:
import React from 'react';
import { View, StyleSheet } from 'react-native';
import LinearGradient from 'react-native-linear-gradient';
const GradientExample = () => {
return (
<LinearGradient
colors={['#4facfe', '#00f2fe']}
style={styles.gradient}
>
{/* Your content here */}
</LinearGradient>
);
};
const styles = StyleSheet.create({
gradient: {
flex: 1,
justifyContent: 'center',
alignItems: 'center',
},
});
Three-Color Gradient
Add more colors for richer gradients:
<LinearGradient
colors={['#667eea', '#764ba2', '#f093fb']}
style={styles.gradient}
>
{/* Content */}
</LinearGradient>
Gradient with Custom Styling
Combine gradients with other styles:
<LinearGradient
colors={['#fa709a', '#fee140']}
style={[styles.gradient, styles.customStyle]}
>
<Text style={styles.text}>Gradient Text</Text>
</LinearGradient>
Gradient Directions and Angles
Vertical Gradient (Top to Bottom)
Default direction - colors transition from top to bottom:
<LinearGradient
colors={['#ff6b6b', '#4ecdc4']}
start={{ x: 0, y: 0 }}
end={{ x: 0, y: 1 }}
style={styles.gradient}
>
Horizontal Gradient (Left to Right)
Colors transition from left to right:
<LinearGradient
colors={['#ff6b6b', '#4ecdc4']}
start={{ x: 0, y: 0 }}
end={{ x: 1, y: 0 }}
style={styles.gradient}
>
Diagonal Gradients
Create diagonal gradients by adjusting start and end points:
Top-left to Bottom-right:
<LinearGradient
colors={['#667eea', '#764ba2']}
start={{ x: 0, y: 0 }}
end={{ x: 1, y: 1 }}
style={styles.gradient}
>
Top-right to Bottom-left:
<LinearGradient
colors={['#667eea', '#764ba2']}
start={{ x: 1, y: 0 }}
end={{ x: 0, y: 1 }}
style={styles.gradient}
>
Custom Angle Gradients
For precise angles, calculate coordinates:
// 45-degree angle
const angle = 45;
const radians = (angle * Math.PI) / 180;
const startX = 0.5 - Math.cos(radians) * 0.5;
const startY = 0.5 - Math.sin(radians) * 0.5;
const endX = 0.5 + Math.cos(radians) * 0.5;
const endY = 0.5 + Math.sin(radians) * 0.5;
<LinearGradient
colors={['#ff6b6b', '#4ecdc4']}
start={{ x: startX, y: startY }}
end={{ x: endX, y: endY }}
style={styles.gradient}
>
Working with Colors
Color Formats
React Native Linear Gradient accepts various color formats:
Hex Colors:
colors={['#FF0000', '#00FF00', '#0000FF']}
RGB Colors:
colors={['rgb(255, 0, 0)', 'rgb(0, 255, 0)', 'rgb(0, 0, 255)']}
RGBA Colors (with transparency):
colors={['rgba(255, 0, 0, 0.8)', 'rgba(0, 255, 0, 0.6)']}
Named Colors (limited support):
colors={['red', 'blue', 'green']}
Gradient with Transparency
Create fade effects using transparent colors:
<LinearGradient
colors={['rgba(0,0,0,0.8)', 'rgba(0,0,0,0)']}
style={styles.overlay}
>
<Text style={styles.overlayText}>Overlay Text</Text>
</LinearGradient>
Dynamic Color Themes
Use theme colors dynamically:
const theme = {
primary: '#667eea',
secondary: '#764ba2',
};
<LinearGradient
colors={[theme.primary, theme.secondary]}
style={styles.gradient}
>
Color Locations and Stops
The locations prop controls where each color appears in the gradient (0.0 to 1.0):
Custom Color Stops
<LinearGradient
colors={['#ff6b6b', '#4ecdc4', '#45b7d1']}
locations={[0, 0.5, 1]}
style={styles.gradient}
>
This places:
- First color at 0% (start)
- Second color at 50% (middle)
- Third color at 100% (end)
Creating Color Bands
Use locations to create distinct color bands:
<LinearGradient
colors={['#ff6b6b', '#ff6b6b', '#4ecdc4', '#4ecdc4']}
locations={[0, 0.3, 0.3, 1]}
style={styles.gradient}
>
This creates a sharp transition at 30%.
Smooth vs Sharp Transitions
Smooth transition (default):
colors={['#ff6b6b', '#4ecdc4']}
// Colors blend smoothly
Sharp transition:
colors={['#ff6b6b', '#ff6b6b', '#4ecdc4', '#4ecdc4']}
locations={[0, 0.5, 0.5, 1]}
// Sharp line at 50%
Common Use Cases
Gradient Buttons
Create attractive gradient buttons:
const GradientButton = ({ onPress, title }) => {
return (
<TouchableOpacity onPress={onPress}>
<LinearGradient
colors={['#667eea', '#764ba2']}
start={{ x: 0, y: 0 }}
end={{ x: 1, y: 0 }}
style={styles.button}
>
<Text style={styles.buttonText}>{title}</Text>
</LinearGradient>
</TouchableOpacity>
);
};
const styles = StyleSheet.create({
button: {
paddingVertical: 12,
paddingHorizontal: 24,
borderRadius: 8,
alignItems: 'center',
},
buttonText: {
color: '#fff',
fontSize: 16,
fontWeight: 'bold',
},
});
Card Headers
Add gradient headers to cards:
<View style={styles.card}>
<LinearGradient
colors={['#fa709a', '#fee140']}
style={styles.cardHeader}
>
<Text style={styles.cardTitle}>Card Title</Text>
</LinearGradient>
<View style={styles.cardContent}>
{/* Card content */}
</View>
</View>
Background Gradients
Full-screen or container background gradients:
<LinearGradient
colors={['#667eea', '#764ba2']}
style={StyleSheet.absoluteFillObject}
>
{/* Screen content */}
</LinearGradient>
Image Overlays
Add gradient overlays on images:
<View style={styles.imageContainer}>
<Image source={imageSource} style={styles.image} />
<LinearGradient
colors={['rgba(0,0,0,0)', 'rgba(0,0,0,0.8)']}
style={styles.overlay}
>
<Text style={styles.overlayText}>Image Caption</Text>
</LinearGradient>
</View>
Status Bar Gradients
Match status bar with gradient:
<LinearGradient
colors={['#667eea', '#764ba2']}
style={styles.statusBar}
>
<StatusBar barStyle="light-content" />
</LinearGradient>
Advanced Techniques
Animated Gradients
Animate gradient colors using Animated API:
import { Animated } from 'react-native';
const AnimatedLinearGradient = Animated.createAnimatedComponent(LinearGradient);
const AnimatedGradient = () => {
const color1 = useRef(new Animated.Value(0)).current;
const color2 = useRef(new Animated.Value(0)).current;
useEffect(() => {
Animated.loop(
Animated.sequence([
Animated.timing(color1, {
toValue: 1,
duration: 2000,
useNativeDriver: false,
}),
Animated.timing(color2, {
toValue: 1,
duration: 2000,
useNativeDriver: false,
}),
])
).start();
}, []);
const interpolateColor1 = color1.interpolate({
inputRange: [0, 1],
outputRange: ['#667eea', '#f093fb'],
});
const interpolateColor2 = color2.interpolate({
inputRange: [0, 1],
outputRange: ['#764ba2', '#f5576c'],
});
return (
<AnimatedLinearGradient
colors={[interpolateColor1, interpolateColor2]}
style={styles.gradient}
/>
);
};
Gradient Text
Create gradient text effect:
import { MaskedViewIOS } from '@react-native-community/masked-view';
const GradientText = ({ children, style }) => {
return (
<MaskedViewIOS
maskElement={<Text style={style}>{children}</Text>}
>
<LinearGradient
colors={['#667eea', '#764ba2']}
start={{ x: 0, y: 0 }}
end={{ x: 1, y: 0 }}
>
<Text style={[style, { opacity: 0 }]}>{children}</Text>
</LinearGradient>
</MaskedViewIOS>
);
};
Multiple Gradient Layers
Layer multiple gradients for complex effects:
<View style={styles.container}>
<LinearGradient
colors={['#667eea', '#764ba2']}
style={StyleSheet.absoluteFillObject}
/>
<LinearGradient
colors={['rgba(255,255,255,0.2)', 'rgba(255,255,255,0)']}
start={{ x: 0, y: 0 }}
end={{ x: 0, y: 1 }}
style={StyleSheet.absoluteFillObject}
/>
{/* Content */}
</View>
Performance Optimization
Reusable Gradient Components
Create reusable gradient components to avoid re-rendering:
const GradientBackground = React.memo(({ children }) => {
return (
<LinearGradient
colors={['#667eea', '#764ba2']}
style={styles.background}
>
{children}
</LinearGradient>
);
});
Optimize Gradient Calculations
Pre-calculate gradient coordinates:
const GRADIENT_CONFIG = {
vertical: {
start: { x: 0, y: 0 },
end: { x: 0, y: 1 },
},
horizontal: {
start: { x: 0, y: 0 },
end: { x: 1, y: 0 },
},
};
<LinearGradient
colors={['#667eea', '#764ba2']}
{...GRADIENT_CONFIG.vertical}
style={styles.gradient}
>
Use useMemo for Dynamic Gradients
Memoize gradient configurations:
const gradientConfig = useMemo(() => ({
colors: theme === 'dark'
? ['#1a1a1a', '#2d2d2d']
: ['#ffffff', '#f0f0f0'],
start: { x: 0, y: 0 },
end: { x: 0, y: 1 },
}), [theme]);
Troubleshooting
Common Issues
Gradient not showing:
- Ensure
react-native-linear-gradientis properly installed - Check that colors array has at least 2 colors
- Verify style prop includes dimensions (width/height or flex)
iOS build errors:
- Run
cd ios && pod install - Clean build folder: Product → Clean Build Folder
- Delete DerivedData folder
Android build errors:
- Clean project:
cd android && ./gradlew clean - Rebuild:
npx react-native run-android
Performance issues:
- Avoid animating gradients on every frame
- Use
React.memofor gradient components - Pre-calculate gradient configurations
Best Practices
- Use consistent color palettes across your app
- Test gradients on both iOS and Android - they may render slightly differently
- Consider accessibility - ensure text is readable over gradients
- Optimize for performance - avoid unnecessary re-renders
- Document gradient configurations - keep track of color schemes
Conclusion
Linear gradients are a powerful tool for enhancing React Native app interfaces. By understanding:
- Basic gradient implementation
- Direction and angle control
- Color management
- Common use cases
- Performance optimization
You can create beautiful, modern interfaces that stand out. Remember to:
- Test on both platforms
- Optimize for performance
- Maintain consistent design language
- Consider accessibility
With react-native-linear-gradient, you have everything you need to add professional-grade gradients to your React Native applications.
Written by Mahesh Waghmare
I bridge the gap between WordPress architecture and modern React frontends. Currently building tools for the AI era.
Follow on Twitter →