Created
February 3, 2023 02:33
-
-
Save DigitalZebra/ab908dacb6368c97520c8f427b77bb56 to your computer and use it in GitHub Desktop.
Custom Tamagui component w/ required props
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import React, {forwardRef} from 'react'; | |
import {GetProps, Stack, styled, Text} from '@tamagui/core'; | |
import {View} from 'react-native'; | |
export const StyledFrame = styled(Stack, { | |
padding: 20, | |
backgroundColor: '$green7Dark', | |
}); | |
type RequiredProps = { | |
displayValue: string; | |
}; | |
/* required props unioned w/ styled Stack */ | |
type ComponentProps = RequiredProps & GetProps<typeof TestButtonFrame>; | |
export const CustomComponent = forwardRef<View, ComponentProps>( | |
function MyTouchable({displayValue, ...rest}, ref) { | |
return ( | |
<StyledFrame {...rest} ref={ref}> | |
<Text color="white">{displayValue}</Text> | |
</StyledFrame> | |
); | |
}, | |
); | |
/** | |
* I have to specify `displayValue` here - | |
* though I'd like to omit it here, and instead specify it in JSX | |
*/ | |
const StyledCustomComponent = styled(CustomComponent, { | |
margin: 16, | |
}); | |
{/* This would be a more ideal location to specify `displayValue` */} | |
<StyledCustomComponent displayValue="Hello world" />; | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment