-
Notifications
You must be signed in to change notification settings - Fork 166
/
Copy pathhoc.ts
92 lines (84 loc) · 2.17 KB
/
hoc.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
import {
createElement,
ComponentType,
ComponentPropsWithRef,
FC,
ReactElement,
useContext,
useEffect,
useState
} from 'https://esm.sh/react@17.0.2'
import { FallbackContext } from './context.ts'
import { isLikelyReactComponent } from './helper.ts'
import { useRouter } from './hooks.ts'
/**
* `withRouter` injects the prop as current `RouterURL` of page routing.
*
* ```tsx
* class MyComponent extends React.Component {
* render() {
* return <p>{this.props.router.pathname}</p>
* }
* }
* export default withRouter(MyComponent)
* ```
*/
export function withRouter<P>(Component: ComponentType<P>) {
return function WithRouter(props: P) {
const router = useRouter()
return createElement(Component, { ...props, router })
}
}
/**
* `dynamic` loads a component asynchronously that is ignored at build time(SSR).
*
* ```jsx
* const MyLogo = dynamic(() => import('~/components/logo.tsx'))
* export default function Logo() {
* return (
* <Fallback to={<p>loading...</p>}>
* <MyLogo />
* </Fallback>
* )
* }
* ```
*/
export function dynamic<T extends ComponentType<any>>(
factory: () => Promise<{ default: T }>
): ComponentType<ComponentPropsWithRef<T>> {
const DynamicComponent: FC<ComponentPropsWithRef<T>> = props => {
const [mod, setMod] = useState<{ component: T } | null>(null)
const [err, setErr] = useState<Error | null>(null)
const { to } = useContext(FallbackContext)
useEffect(() => {
factory().then(({ default: component }) => {
if (isLikelyReactComponent(component)) {
setMod({ component })
} else {
setErr(new Error('Missing the react component exported as default'))
}
}).catch(err => {
setErr(err)
console.error(err.stack)
})
}, [])
if (err !== null) {
return createElement(
'span',
{
style: {
color: 'red',
fontWeight: 'bold'
}
},
err.message
)
}
if (mod !== null) {
return createElement(mod.component, props)
}
return to as ReactElement
}
DynamicComponent.displayName = 'Dynamic'
return DynamicComponent
}