forked from mobxjs/mobx
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathObserverComponent.ts
More file actions
54 lines (49 loc) · 1.42 KB
/
ObserverComponent.ts
File metadata and controls
54 lines (49 loc) · 1.42 KB
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
import { useObserver } from "./useObserver"
interface IObserverProps {
children?(): React.ReactElement | null
render?(): React.ReactElement | null
}
function ObserverComponent({ children, render }: IObserverProps) {
const component = children || render
if (typeof component !== "function") {
return null
}
return useObserver(component)
}
if ("production" !== process.env.NODE_ENV) {
ObserverComponent.propTypes = {
children: ObserverPropsCheck,
render: ObserverPropsCheck
}
}
ObserverComponent.displayName = "Observer"
export { ObserverComponent as Observer }
function ObserverPropsCheck(
props: { [k: string]: any },
key: string,
componentName: string,
location: any,
propFullName: string
) {
const extraKey = key === "children" ? "render" : "children"
const hasProp = typeof props[key] === "function"
const hasExtraProp = typeof props[extraKey] === "function"
if (hasProp && hasExtraProp) {
return new Error(
"MobX Observer: Do not use children and render in the same time in`" + componentName
)
}
if (hasProp || hasExtraProp) {
return null
}
return new Error(
"Invalid prop `" +
propFullName +
"` of type `" +
typeof props[key] +
"` supplied to" +
" `" +
componentName +
"`, expected `function`."
)
}