Skip to content

Commit 6f5f0c7

Browse files
committed
Merge pull request #166 from bryceosterhaus/master
Add section for namespaced components
2 parents 373f207 + b9ad5f9 commit 6f5f0c7

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed

ADVANCED.md

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ The best tool for creating React + TS libraries right now is [`tsdx`](https://gi
7373
- [Section 3: Misc. Concerns](#section-3-misc-concerns)
7474
- [Writing TypeScript Libraries instead of Apps](#writing-typescript-libraries-instead-of-apps)
7575
- [Commenting Components](#commenting-components)
76+
- [Namespaced Components](#namespaced-components)
7677
- [Design System Development](#design-system-development)
7778
- [Migrating from Flow](#migrating-from-flow)
7879
- [Prettier](#prettier)
@@ -1134,6 +1135,33 @@ export default function MyComponent({ label = "foobar" }: MyProps) {
11341135
11351136
[Something to add? File an issue](https://github.com/typescript-cheatsheets/react-typescript-cheatsheet/issues/new).
11361137
1138+
## Namespaced Components
1139+
1140+
Often when creating similar components or components that have a parent-child relationship, it is useful to namespace your components. Types can easily be added be using `Object.assign()`;
1141+
1142+
```tsx
1143+
import React from 'react'
1144+
1145+
const Input = (props: any) => <input {...props} />
1146+
1147+
const Form = React.forwardRef<HTMLDivElement, any>(({children, ...otherProps}, ref) => (
1148+
<form {...otherProps} ref={ref}>
1149+
{children}
1150+
</form>
1151+
));
1152+
1153+
/**
1154+
* Exported components now can be used as `<Form>` and `<Form.Input>`
1155+
*/
1156+
export default Object.assign(Form, {Input: Input});
1157+
```
1158+
1159+
[View in the TypeScript Playground](https://www.typescriptlang.org/play/?jsx=2&ssl=1&ssc=1&pln=14&pc=52#code/JYWwDg9gTgLgBAJQKYEMDG8BmUIjgcilQ3wFgAoCtCAOwGd4BJGsAV3gF44AKMHMOgC44KGgE8AlHA4A+OAB5gLdnADeAOk18IAgL5wA9DIpVaDOADFoeLsnQx1maAHcUUACbJM8gBIAVAFkAGQARYAA3AFEAGyQQJBoYABoRcRlublU0AAtgaPciGhTNdQgYbKQoAAV+Ol0UokwpWR4KOAUnKDwNTTKK6tr9Ro5VRt1jcnb2rNz8wt02hQNOkAmJCQBuE3IDACpdtt24SIAPSFgkdzhqcFoEmDo4Gghna9E4ACMkOFY6S5FHgADeRWLoyQGpK7A0EgdTMNgwcGHAwUJBnaDwdxITAoVjReAAeQ+ACskBh1Cg6HRgABzGjcGEpVTw9jCFkwXSbIA)
1160+
1161+
(Contributed by @bryceosterhaus, see [further discussion](https://github.com/typescript-cheatsheets/react-typescript-cheatsheet/issues/165))
1162+
1163+
[Something to add? File an issue](https://github.com/typescript-cheatsheets/react-typescript-cheatsheet/issues/new).
1164+
11371165
## Design System Development
11381166
11391167
I do like [Docz](https://docz.site/) which takes basically [1 line of config](https://www.docz.site/documentation/project-configuration#typescript) to accept Typescript. However it is newer and has a few more rough edges (many breaking changes since it is still < v1.0)

0 commit comments

Comments
 (0)