javascriptreactjsreact-nativeshadowbox-shadow

React Native: Apply shadow to outside of View and do not apply to inner views


This is what my page looks like

enter image description here

I am trying to get a shadow only where the red line is

enter image description here

I don't want shadow on the inner elements, especially not the text. I also want the shadow to appear below the View.

This is how I am styling my View

  myOuterView: {
    borderWidth: 1,
    borderRadius: 2,
    borderColor: '#ddd',
    borderBottomWidth: 0,
    shadowColor: '#000',
    shadowOffset: { width: 0, height: 1 },
    shadowOpacity: 0.8,
    shadowRadius: 2,
    elevation: 1,
    marginLeft: 5,
    marginRight: 5,
    marginTop: 10,

  },

Answers on this solution did not work or caused other problems


Solution

  • See below example which i created a Shadow box for both ios and android. Its a little bit tricky when it comes to 'ios' and i have clearly shown that in the example below. i think this will help you.

    import React, { Component } from 'react';
    import { View, Text, Dimensions, Platform } from 'react-native';
    
    const screenHieght = Dimensions.get('window').height;
    class ShadowBox extends Component {
      render() {
        return (
          <View style={{ flex: 1, alignItems: 'center', justifyContent: 'center' }}>
    
            <View style={styles.innerView}>
              <View style={styles.outer}>
                <Text style={{ textAlign: 'center' }}>card </Text>
              </View>
              <View style={styles.outer}>
                <Text style={{ textAlign: 'center' }}>card </Text>
              </View>
            </View>
    
          </View>
        );
      }
    }
    
    const styles = {
      innerView: {
        borderWidth: 1,
        backgroundColor: Platform.OS === 'ios' ? '#fff' : null,
        borderColor: '#ddd',
        shadowColor: Platform.OS === 'ios' ? 'green' : '#fff',
        shadowOffset: {
          width: Platform.OS === 'ios' ? 3 : 0,
          height: Platform.OS === 'ios' ? 3 : 2,
        },
        shadowOpacity: Platform.OS === 'ios' ? 1 : 0.8,
        shadowRadius: Platform.OS === 'ios' ? null : 40,
        elevation: Platform.OS === 'ios' ? null : 4,
        justifyContent: 'center',
        alignItems: 'center',
        width: 300,
        height: 300,
      },
      outer: {
        margin: 10,
        padding: 10,
        alignSelf: 'center',
        borderWidth: 1,
        width: '80%',
        overflow: 'hidden',
      },
    };
    
    export default ShadowBox;
    

    Change this according to your requirement. Feel free to ask me any doubts that you might have.