【问题标题】:How do I set width(100%) the same as it's parent without using a string in React Native?如何在不使用 React Native 中的字符串的情况下将宽度(100%)设置为与其父级相同?
【发布时间】:2019-02-15 09:49:04
【问题描述】:

我试图让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>

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

【问题讨论】:

    标签: css string reactjs react-native jsx


    【解决方案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 的写法。

    【讨论】:

    • 非常感谢!我觉得在return 之前做这件事只是一个额外的步骤,所以我只是把等式插入到cropWidth 中。是的,ImageZoom 很奇怪。再次感谢您!
    猜你喜欢
    • 2017-09-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-08-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多