-
Notifications
You must be signed in to change notification settings - Fork 29.8k
/
WithRouter.tsx
65 lines (50 loc) · 1.95 KB
/
WithRouter.tsx
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
import * as React from 'react';
import { withRouter, RouteComponentProps } from 'react-router-dom';
interface TOwnProps extends RouteComponentProps {
username: string;
}
const ComponentFunction = (props: TOwnProps) => (
<h2>Welcome {props.username}</h2>
);
const FunctionComponent: React.FunctionComponent<TOwnProps> = props => (
<h2>Welcome {props.username}</h2>
);
declare const Component: React.ComponentType<TOwnProps>;
class ComponentClass extends React.Component<TOwnProps> {
render() {
return <h2>Welcome {this.props.username}</h2>;
}
}
const WithRouterComponentFunction = withRouter(ComponentFunction);
const WithRouterFunctionComponent = withRouter(FunctionComponent);
const WithRouterComponent = withRouter(Component);
const WithRouterComponentClass = withRouter(ComponentClass);
WithRouterComponentClass.WrappedComponent; // $ExpectType typeof ComponentClass
const WithRouterTestFunction = () => (
<WithRouterComponentFunction username="John" />
);
const OnWrappedRefFn = (ref: ComponentClass | null) => {};
const WithRouterTestClass = () => <WithRouterComponentClass username="John" wrappedComponentRef={OnWrappedRefFn} />;
const OnWrappedRef = React.createRef<ComponentClass>();
const WithRouterTestClass2 = () => <WithRouterComponentClass username="John" wrappedComponentRef={OnWrappedRef} />;
// union props
{
interface Book {
kind: 'book';
author: string;
}
interface Magazine {
kind: 'magazine';
issue: number;
}
type SomethingToRead = (Book | Magazine) & RouteComponentProps;
const Readable: React.SFC<SomethingToRead> = props => {
if (props.kind === 'magazine') {
return <div>magazine #{props.issue}</div>;
}
return <div>magazine #{props.author}</div>;
};
const RoutedReadable = withRouter(Readable);
<RoutedReadable kind="book" author="Hejlsberg" />;
<RoutedReadable kind="magazine" author="Hejlsberg" />; // $ExpectError
}