css - 不变违规:提供给“样式表生成”预期“数字”的“字符串”类型的无效道具“字体大小” - 样式组件 RN
问题描述
不知道我哪里出错了,我知道 React native 不支持单位,但不支持样式组件来处理这种转换。每次我运行它时,它都会抛出一个错误“样式表生成的值作为字符串,但预期是数字”。当我查看 styled-components 文档时,这种语法似乎很好。
const HomeContainer = styled.View`
flex: 1;
background-color: hsl(202, 8%, 90%);
`;
const TaskWrapper = styled.View`
padding-top: 5rem;
padding-left: 1.2rem;
`;
const SectionTitle = styled.Text`
color: hsl(0, 25%, 90%);
font-size: 1.5rem;
`;
const Home = (props) => {
return (
<HomeContainer theme={props.theme}>
<TaskWrapper>
<SectionTitle theme={props.theme}> Today's Tasks </SectionTitle
</TaskWrapper>
</HomeContainer>
);
};
export default Home;
错误 :
Running application on sdk_gphone_x86.
Invariant Violation: Invalid prop `fontSize` of type `string` supplied to `StyleSheet generated`, expected `number`.
StyleSheet generated: {
"color": "hsl(0, 25%, 90%)",
"fontSize": "1.5rem"
}
This error is located at:
in StyledNativeComponent (created by Styled(Text))
in Styled(Text) (at home.js:27)
解决方案
目前你不能在 react-native 中rem
使用相对单位。em
相反,您可以使用px
value 来定义您的 fontSize 并将该值用作Number
.
您的代码可能看起来像 -
const SectionTitle = styled.Text`
color: hsl(0, 25%, 90%);
font-size: 16;
`;
注意:我假设您的fontSize
值为16px
.
推荐阅读
- django - Django - 用户表单中 READ COMMITTED 隔离级别的含义
- plot - 绘图顺序和所有绘制元素,如 line.new 或 label.new
- javascript - 前端 E_VALIDATION_FAILURE 上的 AdonisJS 验证
- perl - 无法合并所需格式的文件
- r - 是否可以使用 runtime: shiny 在 Rmarkdown 文档中设置主机和端口?
- reactjs - 如何重构 $expr、$regexMatch 过滤器以便于阅读 React/MongoDB?
- ios - FCM - 使用 expo (React native) 向 iOS 设备发送通知
- android - FAILURE:构建失败,settings.gradle 中出现异常
- javascript - react native run-android 给出错误
- javascript - 在 Angular 中使用 ngClass 或 ngStyle 动态更改颜色