[英]Using react-native-vector-icons/FontAwesome with styled-components
I'm refactoring some React Native code to use styled-components , but having trouble styling FontAwesome icons. 我正在重构一些React Native代码以使用样式组件 ,但是样式FontAwesome图标遇到了麻烦。 I get an error such as
我收到一个错误,例如
styledComponents2.default.IconFontAwesome is not a function
styledComponents2.default.IconFontAwesome不是函数
so I understand per this GitHub issue that I need to wrap the component and pass the className
prop down per the docs on extending custom components . 因此,对于这个GitHub问题 ,我了解到我需要包装该组件,并根据扩展自定义组件的文档向下传递
className
属性。 So I have this: 所以我有这个:
import React from 'react';
import {
Text,
TouchableOpacity,
} from 'react-native';
import PropTypes from 'prop-types';
import IconFontAwesome from 'react-native-vector-icons/FontAwesome';
import styled from 'styled-components';
const StyledTouchableOpacity = styled.TouchableOpacity`
width: ${props => (props.large ? '100%' : '80%')};
height: ${props => (props.large ? '80px' : '60px')};
font-size: ${props => (props.large ? '18px' : '15px')};
background: '#f1d746';
display: flex;
justify-content: center;
align-items: center;
padding: 20px;
`;
const StyledText = styled.Text`
font-size: 15;
font-weight: bold;
`;
const StylableIcon = ({ className }) => (
<IconFontAwesome className={className} />
);
const StyledIconFontAwesome = styled.StylableIcon`
font-size: 15;
padding: 10px;
`;
const Button = (props) => {
let _icon = null;
if (props.icon) {
_icon = (
<StyledIconFontAwesome name={props.icon} />
);
}
return (
<StyledTouchableOpacity>
<StyledText>{props.text}</StyledText>
{_icon}
</StyledTouchableOpacity>
);
};
Button.defaultProps = {
icon: null,
};
Button.propTypes = {
icon: PropTypes.string,
};
export default Button;
which results in a similar error 导致类似的错误
styledComponents2.default.StylableIcon is not a function
styledComponents2.default.StylableIcon不是一个函数
Any tips on what I'm doing wrong here? 关于我在这里做错什么的任何提示? Thanks all.
谢谢大家
OK got it working with: OK可以使用:
import React from 'react';
import {
Text,
TouchableOpacity,
} from 'react-native';
import PropTypes from 'prop-types';
import IconFontAwesome from 'react-native-vector-icons/FontAwesome';
import styled from 'styled-components';
const StyledTouchableOpacity = styled.TouchableOpacity`
width: ${props => (props.large ? '100%' : '80%')};
height: ${props => (props.large ? '80px' : '60px')};
font-size: ${props => (props.large ? '18px' : '15px')};
background: '#f1d746';
display: flex;
justify-content: center;
align-items: center;
padding: 20px;
`;
const StyledText = styled.Text`
font-size: 15;
font-weight: bold;
`;
const StyledIconFontAwesome = styled(IconFontAwesome)`
font-size: 15;
padding: 10px;
`;
const Button = (props) => {
let _icon = null;
if (props.icon) {
_icon = (
<StyledIconFontAwesome name={props.icon} />
);
}
return (
<StyledTouchableOpacity>
<StyledText>{props.text}</StyledText>
{_icon}
</StyledTouchableOpacity>
);
};
Button.defaultProps = {
icon: null,
};
Button.propTypes = {
icon: PropTypes.string,
};
export default Button;
So two fixes here: 所以这里有两个解决方法:
styled(Something)
as opposed to styled.Something
(thanks @bennygenel) styled(Something)
而不是styled.Something
的语法(感谢@bennygenel) StylableIcon
was unnecessary. StylableIcon
是不必要的。 I can call styled(IconFontAwesome)
directly. styled(IconFontAwesome)
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.