【发布时间】:2019-07-31 13:46:32
【问题描述】:
我正在学习 React Native 和样式化组件。我正在开发一个简单的 iOS 应用程序,但遇到了styled-components 的一些问题。
我要做什么
我试图在点击时显示modal,看起来像这样
<Modal visible={this.state.isModalVisible} animationType={'fade'}>
<StyledView flex={1} padding={10} backgroundColor={'orange'}>
<View>
...more Views and Texts
</View>
</StyledView>
</Modal>
StyledView 是我使用styled-components 创建的自定义视图,如下所示
const ViewWrapper = styled.View`
flex: ${props => props.flex};
padding: ${props => props.padding};
backgroundColor: ${props => props.backgroundColor};
`;
const StyledView = ({ flex, padding, backgroundColor }) => (
<ViewWrapper
flex={flex}
padding={padding}
backgroundColor={backgroundColor}
/>
);
export default StyledView;
我遇到的问题
1) 当我设置padding={10} 时,我得到一个错误Failed to parse declaration "padding: 10"。
2) 谷歌搜索了一下后,我发现我应该使用padding={'10px'},它会抛出这个错误,10px is of type NSString cannot be converted to YGValue. Did you forget the % or pt suffix?。
(padding={'10%'} 工作正常)
然后我只是尝试在 ViewWrapper 中设置 flex 和 padding 值,并且只发送背景颜色为 prop。
3) 但由于某种原因,嵌套在StyledView 中的Views 和Texts 没有显示出来。
请告诉我为什么它不起作用,并帮助我了解我在这里缺少什么。谢谢。
【问题讨论】:
标签: react-native styled-components