mirror of
https://github.com/gosticks/DefinitelyTyped.git
synced 2026-12-04 20:21:46 +00:00
[react-router] add RouteComponentProps.staticContext (#16812)
* [react-router] add RouteComponentProps.staticContext * [react-router] revert RouteConfig test file * [react-router] Add example for StaticRouter and staticContext
This commit is contained in:
committed by
Mohamed Hegazy
parent
895a8920bc
commit
3b1ba96df2
Vendored
+1
@@ -59,6 +59,7 @@ export interface RouteComponentProps<P> {
|
||||
match: match<P>;
|
||||
location: H.Location;
|
||||
history: H.History;
|
||||
staticContext?: any;
|
||||
}
|
||||
|
||||
export interface RouteProps {
|
||||
|
||||
@@ -0,0 +1,55 @@
|
||||
import * as React from 'react';
|
||||
import { StaticRouter, Route } from 'react-router-dom';
|
||||
|
||||
interface StaticContext {
|
||||
statusCode?: number;
|
||||
}
|
||||
|
||||
interface RouteStatusProps {
|
||||
statusCode: number;
|
||||
}
|
||||
|
||||
const RouteStatus: React.SFC<RouteStatusProps> = (props) => (
|
||||
<Route
|
||||
render={({ staticContext }) => {
|
||||
if (staticContext) {
|
||||
(staticContext as StaticContext).statusCode = props.statusCode;
|
||||
}
|
||||
|
||||
return (
|
||||
<div>
|
||||
{props.children}
|
||||
</div>
|
||||
);
|
||||
}}
|
||||
/>
|
||||
);
|
||||
|
||||
interface PrintContextProps {
|
||||
staticContext: StaticContext;
|
||||
}
|
||||
|
||||
const PrintContext: React.SFC<PrintContextProps> = (props) => (
|
||||
<p>
|
||||
Static context: {JSON.stringify(props.staticContext)}
|
||||
</p>
|
||||
);
|
||||
|
||||
class StaticRouterExample extends React.Component<{}, {}> {
|
||||
staticContext: StaticContext = {};
|
||||
|
||||
render() {
|
||||
return (
|
||||
<StaticRouter location="/foo" context={this.staticContext}>
|
||||
<div>
|
||||
<RouteStatus statusCode={404}>
|
||||
<p>Route with statusCode 404</p>
|
||||
<PrintContext staticContext={this.staticContext} />
|
||||
</RouteStatus>
|
||||
</div>
|
||||
</StaticRouter>
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
export default StaticRouterExample;
|
||||
@@ -29,6 +29,7 @@
|
||||
"test/Recursive.tsx",
|
||||
"test/RouteConfig.tsx",
|
||||
"test/Sidebar.tsx",
|
||||
"test/StaticRouter.tsx",
|
||||
"test/Switch.tsx",
|
||||
"test/WithRouter.tsx"
|
||||
]
|
||||
|
||||
Reference in New Issue
Block a user