Onjsdev

Share


How To Create Shadow Effect In React Native


By onjsdev

Jan 24th, 2024

Creating shadows in React Native involves using the elevation style property for Android and the shadow properties for iOS.

Here's a guide to creating shadows in React Native:

Android (using elevation)

To create shadow effect in android, you need to use the elavation property. Here is a simple component with shadow effect in android devices:

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

const App = () => {
  return (
    <View style={styles.container}>
      <View style={styles.shadowBox}>
        <Text>Shadow Example</Text>
      </View>
    </View>
  );
};

const styles = StyleSheet.create({
  container: {
    flex: 1,
    justifyContent: 'center',
    alignItems: 'center',
  },
  shadowBox: {
    width: 200,
    height: 100,
    backgroundColor: 'white',
    elevation: 5, // Elevation for shadow on Android
    borderRadius: 10,
    padding: 10,
  },
});

export default App;

Here is the result:

React Native Shadow Effect In Android

iOS (using shadow properties):

In IOS, the shadow properties (shadowColor, shadowOffset, shadowOpacity, and shadowRadius) are set to create a shadow effect.

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

const App = () => {
  return (
    <View style={styles.container}>
      <View style={styles.shadowBox}>
        <Text>Shadow Example</Text>
      </View>
    </View>
  );
};

const styles = StyleSheet.create({
  container: {
    flex: 1,
    justifyContent: 'center',
    alignItems: 'center',
  },
  shadowBox: {
    width: 200,
    height: 100,
    backgroundColor: 'white',
    borderRadius: 10,
    padding: 10,
    // Shadow properties for iOS
    shadowColor: '#000',
    shadowOffset: {
      width: 0,
      height: 2,
    },
    shadowOpacity: 0.25,
    shadowRadius: 3.84,
    elevation: 5, // This elevation prop is required for the shadow to work on Android
  },
});

export default App;

Now, when you run your React Native application (npx react-native run-android or npx react-native run-ios), you should see a component with a shadow effect on both Android and iOS devices.

Conclusion

This article shows how to add shadow effect to your React Native components for both Android and IOS devices. I hope this comprehensive guide helps you effectively add shadows to your React Native app!

Thank you for reading.