如何在React Native中不使用字符串的情况下将width(100%)设置为与父级相同的宽度?

时间:2018-09-10 23:06:10

标签: css string reactjs react-native jsx

我正在尝试使cropWidth的宽度与其父宽度相同,但是当我使用字符串时,出现此错误:unrecognized selector sent to instance。这是代码:

<RkCard style={{width:'80%',  marginLeft: 40,  marginBottom: 50, backgroundColor:'#f5f5f5', position:'relative',  shadowColor: 'black', shadowOffset: {width: 10, height: 10}, shadowOpacity: 1, shadowRadius: 2, elevation: 5}}>
       <ImageZoom cropWidth={'100%'} <-- string here, error is thrown
                  cropHeight={300}
                  imageWidth={300}
                  imageHeight={300}
                  style={{left: 0, alignSelf:'center', justifyContent: 'center', alignItems: 'center', backgroundColor:'#D3D3D3',}}>
         <FastImage rkCardImg source={{uri:`https://www.example.com/profiles/uploads/${item.images}`,
         headers:{ Authorization: 'someAuthToken' },
         priority: FastImage.priority.high,
          }}
           resizeMode={FastImage.resizeMode.contain}
           style={{width: '100%', height: 300, left: 0, alignSelf:'center', justifyContent: 'center', alignItems: 'center'}}/>

        </ImageZoom>
        </RkCard>

如果我使用整数,则错误消失,因此它可能仅接受那些。有没有办法用字符串将宽度设置为与父级相同?

1 个答案:

答案 0 :(得分:1)

您可以在return之前自己计算。例如:

let width = Dimensions.get('window').width * .8;

,然后在您的RkCardcropWidth中使用该值,例如:

<RkCard style={{width,  marginLeft: 40,  marginBottom: 50, backgroundColor:'#f5f5f5', position:'relative',  shadowColor: 'black', shadowOffset: {width: 10, height: 10}, shadowOpacity: 1, shadowRadius: 2, elevation: 5}}>
   <ImageZoom cropWidth={width} 

顺便说一句,问题不是React Native风格的问题,而是ImageZoom的编写方式。