-
Notifications
You must be signed in to change notification settings - Fork 156
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
50acd9b
commit ed9bf17
Showing
3 changed files
with
110 additions
and
15 deletions.
There are no files selected for viewing
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
54 changes: 54 additions & 0 deletions
54
src/08-advanced-patterns/67.5-hoc-for-generic-components.problem.tsx
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
import { Router, useRouter } from "fake-external-lib"; | ||
import { Equal, Expect } from "../helpers/type-utils"; | ||
|
||
export const withRouter = <TProps extends { router: Router }>( | ||
Component: React.FC<TProps>, | ||
) => { | ||
const NewComponent = (props: Omit<TProps, "router">) => { | ||
const router = useRouter(); | ||
return <Component {...(props as TProps)} router={router} />; | ||
}; | ||
|
||
NewComponent.displayName = `withRouter(${Component.displayName})`; | ||
|
||
return NewComponent; | ||
}; | ||
|
||
type TableProps<T> = { | ||
data: T[]; | ||
renderRow: (item: T) => React.ReactNode; | ||
router: Router; | ||
}; | ||
|
||
export const Table = <T,>(props: TableProps<T>) => { | ||
return <table />; | ||
}; | ||
|
||
const WrappedTable = withRouter(Table); | ||
|
||
<> | ||
{/* @ts-expect-error router is required! */} | ||
<Table | ||
data={[1, 2, 3]} | ||
renderRow={(row) => { | ||
type test = Expect<Equal<typeof row, number>>; | ||
return <tr />; | ||
}} | ||
/> | ||
|
||
<WrappedTable | ||
data={[1, 2, 3]} | ||
renderRow={(row) => { | ||
type test = Expect<Equal<typeof row, number>>; | ||
return <tr />; | ||
}} | ||
/> | ||
|
||
<WrappedTable | ||
data={[1, 2, 3]} | ||
renderRow={(row) => { | ||
type test = Expect<Equal<typeof row, number>>; | ||
return <tr />; | ||
}} | ||
/> | ||
</>; |
56 changes: 56 additions & 0 deletions
56
src/08-advanced-patterns/67.5-hoc-for-generic-components.solution.tsx
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
import { Router, useRouter } from "fake-external-lib"; | ||
import { Equal, Expect } from "../helpers/type-utils"; | ||
|
||
export const withRouter = <TProps extends { router: Router }>( | ||
Component: (props: TProps) => React.ReactNode, | ||
): ((props: Omit<TProps, "router">) => React.ReactNode) => { | ||
const NewComponent = (props: Omit<TProps, "router">) => { | ||
const router = useRouter(); | ||
return <Component {...(props as TProps)} router={router} />; | ||
}; | ||
|
||
NewComponent.displayName = `withRouter(${ | ||
(Component as { displayName?: string }).displayName | ||
})`; | ||
|
||
return NewComponent; | ||
}; | ||
|
||
type TableProps<T> = { | ||
data: T[]; | ||
renderRow: (item: T) => React.ReactNode; | ||
router: Router; | ||
}; | ||
|
||
export const Table = <T,>(props: TableProps<T>) => { | ||
return <table />; | ||
}; | ||
|
||
const WrappedTable = withRouter(Table); | ||
|
||
<> | ||
{/* @ts-expect-error router is required! */} | ||
<Table | ||
data={[1, 2, 3]} | ||
renderRow={(row) => { | ||
type test = Expect<Equal<typeof row, number>>; | ||
return <tr />; | ||
}} | ||
/> | ||
|
||
<WrappedTable | ||
data={[1, 2, 3]} | ||
renderRow={(row) => { | ||
type test = Expect<Equal<typeof row, number>>; | ||
return <tr />; | ||
}} | ||
/> | ||
|
||
<WrappedTable | ||
data={[1, 2, 3]} | ||
renderRow={(row) => { | ||
type test = Expect<Equal<typeof row, number>>; | ||
return <tr />; | ||
}} | ||
/> | ||
</>; |