React native 在带有样式化组件的react native中应用样式
我有一个组件,徽标,我试图在其中应用宽度和高度:React native 在带有样式化组件的react native中应用样式,react-native,styled-components,React Native,Styled Components,我有一个组件,徽标,我试图在其中应用宽度和高度: import styledNative from "styled-components/native"; import {Image, View} from "react-native" import companyIcon from "../svg/companyIcon.svg"; const Logo = () => ( <Image source={{uri: companyIcon}} />; ); const
import styledNative from "styled-components/native";
import {Image, View} from "react-native"
import companyIcon from "../svg/companyIcon.svg";
const Logo = () => (
<Image source={{uri: companyIcon}} />;
);
const StyledLogo = styledNative(Logo)`
width: 48px;
height: 48px;
`;
const Header = () => {
return (
<View>
<StyledLogo />
</View>
);
};
所以我真的不知道该怎么办。如何通过已设置样式的组件设置组件的样式?您没有将样式应用于图像 现在发生的事情是,您的
徽标
组件正在接收这些样式道具,但它没有传递它们
const Logo = (props) => ( // <-- containing style props
<Image source={{uri: companyIcon}} />;
);
或者,你也可以只通过样式道具
const Logo = (props) => ( // <-- containing style props
<Image source={{uri: companyIcon}} style={props.style}/>;
);
const Logo=(props)=>(//谢谢您的回答-但是传递给Logo的样式对象只是一个两项数组,一项带有数字(91),另一项未定义。如果我这样做:export default({style})=>{return;
我只会得到一个警告:“id91
的样式无效。跳过…”。如果我尝试传递所有道具:导出默认值(道具)=>{return;};
我最终会得到相同的“无效样式”警告:嗯。样式实际上可以是一个数组,可以传入多个,据我所知,使用样式表
创建的样式显示为数字引用,而不是对象。如果我没有弄错的话,您应该能够使用样式表.flattstStyles
来获取样式的实际对象表示。哦,对了。这些样式可能是无效的,因为您正在传递…px
。您的样式应该是RN中没有单位的数字!
const Logo = (props) => ( // <-- containing style props
<Image {...props} source={{uri: companyIcon}} />; // <-- styles now applied to Image
);
const Logo = (props) => ( // <-- containing style props
<Image source={{uri: companyIcon}} style={props.style}/>;
);