Skip to content

Instantly share code, notes, and snippets.

@praskoson
Last active March 15, 2023 13:37
Show Gist options
  • Save praskoson/e0e57e6fd792390a6f0e1340c1f9a869 to your computer and use it in GitHub Desktop.
Save praskoson/e0e57e6fd792390a6f0e1340c1f9a869 to your computer and use it in GitHub Desktop.
Namespaced Components
// 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