[英]Type issue in Typescript React-native styled-component
I am trying to create a TouchableOpacity using styled-component in react-native and was working successfully but since i am using typescript its showing some error when using style flowDirection: 'row'
我正在尝试使用 react-native 中的样式组件创建 TouchableOpacity 并且工作成功,但是由于我使用的是 typescript,因此在使用样式
flowDirection: 'row'
here is my code这是我的代码
interface IDefaultStyle {
style?: object
}
const RootContainer = styled.TouchableOpacity((props: IDefaultStyle) => ({
flexDirection: 'row',
...props.style
}))
And the type error is类型错误是
Argument of type '(props: IDefaultStyle) => { flexDirection: string; } | { flexDirection: string; }' is not assignable to parameter of type 'TemplateStringsArray'.
And when i remove flexDirection
and add any other style type.当我删除
flexDirection
并添加任何其他样式类型时。 the error is gone.错误消失了。
The same issue is seen in styled-components Text
when fontWeight: 'bold'
is given当给定
fontWeight: 'bold'
时,在 styled-components Text
中也会出现同样的问题
My package.json我的package.json
{
"name": "@appmaker/ui",
"version": "0.0.1",
"private": true,
"scripts": {
"android": "react-native run-android",
"ios": "react-native run-ios",
"start": "react-native start",
"test": "jest",
"lint": "eslint ."
},
"peerDependencies": {
"react": "16.9.0",
"react-native": "0.61.2"
},
"devDependencies": {
"@babel/core": "7.6.4",
"@babel/preset-typescript": "^7.7.2",
"@babel/runtime": "7.6.3",
"@react-native-community/eslint-config": "0.0.3",
"@types/jest": "^24.0.20",
"@types/react": "^16.9.11",
"@types/react-native": "^0.60.22",
"@types/react-test-renderer": "^16.9.1",
"@types/styled-components": "^4.1.20",
"@typescript-eslint/eslint-plugin": "^2.5.0",
"@typescript-eslint/parser": "^2.5.0",
"babel-jest": "24.9.0",
"babel-plugin-styled-components": "^1.10.6",
"jest": "24.9.0",
"metro-react-native-babel-preset": "0.51.1",
"prettier": "^1.18.2",
"react-test-renderer": "16.9.0",
"tslint": "^5.20.0",
"tslint-config-prettier": "^1.18.0",
"tslint-config-standard": "^8.0.1",
"tslint-eslint-rules": "^5.4.0",
"tslint-react": "^4.1.0",
"typescript": "^3.6.3",
"typescript-plugin-styled-components": "^1.4.3"
},
"jest": {
"preset": "react-native",
"moduleFileExtensions": [
"ts",
"tsx",
"js",
"jsx",
"json",
"node"
]
},
"bit": {
"env": {
"compiler": "bit.envs/compilers/react-typescript@3.0.27"
},
"componentsDefaultDirectory": "components/{name}",
"packageManager": "npm"
},
"dependencies": {
"i": "^0.3.6",
"npm": "^6.12.0",
"prop-types": "^15.7.2",
"react-native-image-slider": "^2.0.3",
"react-native-image-slider-box": "^1.0.5",
"react-native-snap-carousel": "^3.8.3",
"styled-components": "^4.4.0"
}
}
it will be greatly appreciated if someone could help me with this如果有人可以帮助我,将不胜感激
Edit component usage编辑组件使用
/**
* ## AuthorView | SellerView
* Component to show Author info
* @param props
*/
const AuthorView = (props: IProps) => {
const { name, style, imageUrl, rating, subtext, onPress } = props
// if imageUrl is not provided use default star icon
const imageSource = imageUrl ? { uri: imageUrl } : require('../../icons/star-grey.png')
return(
<RootContainer onPress={onPress} style={style}>
<ImageViewContainer>
<ImageView
defaultImage={imageUrl ? false : true}
source={ imageSource }
/>
</ImageViewContainer>
<RightContainer>
<Name>{name}</Name>
<View style={{ flexDirection: 'row' }}>
{rating && <RatingView style={{ marginRight: 3 }} rating={rating} />}
<SubText>{subtext}</SubText>
</View>
</RightContainer>
</RootContainer>
)
}
export default AuthorView
flexDirection: 'row' as const flexDirection:'行'作为常量
This solved the issue for me.这为我解决了这个问题。
change改变
const RootContainer = styled.TouchableOpacity((props: IDefaultStyle)
=> ({ flexDirection: 'row', ...props.style }))
to至
const RootContainer = styled.TouchableOpacity<IDefaultStyle>`
flexDirection: row;
${...props.style}
`
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.