Quantcast
Channel: Active questions tagged react-native+typescript - Stack Overflow
Viewing all articles
Browse latest Browse all 6287

How to declare type in Stylesheed if style object access like styles[color] in react native typescript?

$
0
0

I am new to typescript. I am trying to create a component in which we pass color name or code and if color exist in the component stylesheet then it will use that style else it will use new color code. Earlier I was using javascript so i didn't required props to define but how i am getting compiler error "componentStyles".

interface Props {  color: string}export default function MyComponent({color, children}: Props){  const componentStyles = [    color && styles[color],    color && !styles[color] && {backgroundColor: color}  ]   return  <View style={componentStyles}>      {children}</View>}interface Styles {  primary: ViewStyle}const styles = StyleSheet.create<Styles>({  primary: {backgroundColor: 'red'}})

I can call component using or

How should i declare Props types for no error from compiler?


Viewing all articles
Browse latest Browse all 6287

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>