Last active
March 15, 2023 13:37
-
-
Save praskoson/e0e57e6fd792390a6f0e1340c1f9a869 to your computer and use it in GitHub Desktop.
Namespaced Components
This file contains 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
// source: https://react-typescript-cheatsheet.netlify.app/docs/advanced/misc_concerns | |
import { forwardRef } from "react"; | |
const Input = (props: any) => <input {...props} />; | |
const Form = forwardRef<HTMLDivElement, any>( | |
({ children, ...otherProps }, ref) => ( | |
<form {...otherProps} ref={ref}> | |
{children} | |
</form> | |
) | |
); | |
/** | |
* Exported components now can be used as `<Form>` and `<Form.Input>` | |
*/ | |
export default Object.assign(Form, { Input: Input }); | |
/* Slightly different approach */ | |
/* export const CustomForm = Object.assign(Form, { Input: Input }); */ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment