[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:
Michael Strobel
2017-05-31 21:56:38 -07:00
committed by Mohamed Hegazy
parent 895a8920bc
commit 3b1ba96df2
3 changed files with 57 additions and 0 deletions
+1
View File
@@ -59,6 +59,7 @@ export interface RouteComponentProps<P> {
match: match<P>;
location: H.Location;
history: H.History;
staticContext?: any;
}
export interface RouteProps {
+55
View File
@@ -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;
+1
View File
@@ -29,6 +29,7 @@
"test/Recursive.tsx",
"test/RouteConfig.tsx",
"test/Sidebar.tsx",
"test/StaticRouter.tsx",
"test/Switch.tsx",
"test/WithRouter.tsx"
]