美文网首页
react-native-boxshadow使用

react-native-boxshadow使用

作者: MasterPaul | 来源:发表于2020-12-23 16:09 被阅读0次

react-native-shadow主页
[https://github.com/879479119/react-native-shadow#readme]
下面自己封装了一下,可以动态计算组件的宽和高

import React, { Component } from 'react'
import { View, Image,Platform } from 'react-native'
import PropTypes from 'prop-types';
import {BoxShadow} from "react-native-shadow";

const defaultOption = {
    color:"#EBEBEB",
    border:3,
    radius:5,
    opacity:0.5,
    x:3,
    y:4,
}

class ShadowBoxView extends Component {

    static propTypes= {

        style:PropTypes.object,
        color:PropTypes.string,
        shadowOption:PropTypes.object
    }


    static defaultProps ={
        shadowOption:defaultOption
    }

    constructor(props){
        super(props);
        this.state={
            contentHeight:0,
            contentWidth:0,
            showShadow:false
        }



    }

    componentDidMount() {

    }

    render(){
        const {color,style,shadowOption} = this.props;

        if(!this.state.showShadow){
            return  (
                <View style={style}
                      onLayout={e=>{
                          const layout = e.nativeEvent.layout
                          // console.log(layout)
                          this.setState({
                              contentWidth:layout.width,
                              contentHeight:layout.height,
                              showShadow:true
                          })
                      }}
                >
                    {this.props.children}
                </View>
            )
        }

        return (
            <BoxShadow
                setting={{
                    height:this.state.contentHeight ,
                    width:this.state.contentWidth,
                    color:color,
                    ...defaultOption,
                    ...shadowOption
                }}
            >
                <View style={style}>
                    {this.props.children}
                </View>

            </BoxShadow>
        );
    }
}
export default ShadowBoxView;

相关文章

网友评论

      本文标题:react-native-boxshadow使用

      本文链接:https://www.haomeiwen.com/subject/onfunktx.html