Skip to content

Instantly share code, notes, and snippets.

@DigitalZebra
Created February 3, 2023 02:33
Show Gist options
  • Save DigitalZebra/ab908dacb6368c97520c8f427b77bb56 to your computer and use it in GitHub Desktop.
Save DigitalZebra/ab908dacb6368c97520c8f427b77bb56 to your computer and use it in GitHub Desktop.
Custom Tamagui component w/ required props
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