Skip to content

[Question] How to properly define namespaced react components? #165

Closed
@bryceosterhaus

Description

@bryceosterhaus

I typically use namespacing in my react components to group similar components. I was wondering what the best approach to defining a type here would be. I generally do something like the example below, however using as ... isn't ideal for me. Any other ideas how to properly type a react component that also has namespaced components?

interface IProps extends React.HTMLAttributes<HTMLDivElement> {
	someProp?: any;
}

type TMyComponent = React.ForwardRefExoticComponent<IProps> & {
	ChildComponent: typeof ChildComponent;
};

import ChildComponent from './ChildComponent';

const MyComponent = React.forwardRef(
	({someProp, ...otherProps}: IProps, ref) => {
		return <div {...otherProps} />;
	}
) as TMyComponent;

MyComponent.ChildComponent = ChildComponent;

export default MyComponent;

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions