From 3b1ba96df2f377c2cbc884d4764c74f867e2187b Mon Sep 17 00:00:00 2001 From: Michael Strobel Date: Thu, 1 Jun 2017 12:56:38 +0800 Subject: [PATCH] [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 --- types/react-router/index.d.ts | 1 + types/react-router/test/StaticRouter.tsx | 55 ++++++++++++++++++++++++ types/react-router/tsconfig.json | 1 + 3 files changed, 57 insertions(+) create mode 100644 types/react-router/test/StaticRouter.tsx diff --git a/types/react-router/index.d.ts b/types/react-router/index.d.ts index 384cb2289e..f1c24b778f 100644 --- a/types/react-router/index.d.ts +++ b/types/react-router/index.d.ts @@ -59,6 +59,7 @@ export interface RouteComponentProps

{ match: match

; location: H.Location; history: H.History; + staticContext?: any; } export interface RouteProps { diff --git a/types/react-router/test/StaticRouter.tsx b/types/react-router/test/StaticRouter.tsx new file mode 100644 index 0000000000..89999d84cb --- /dev/null +++ b/types/react-router/test/StaticRouter.tsx @@ -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 = (props) => ( + { + if (staticContext) { + (staticContext as StaticContext).statusCode = props.statusCode; + } + + return ( +

+ {props.children} +
+ ); + }} + /> +); + +interface PrintContextProps { + staticContext: StaticContext; +} + +const PrintContext: React.SFC = (props) => ( +

+ Static context: {JSON.stringify(props.staticContext)} +

+); + +class StaticRouterExample extends React.Component<{}, {}> { + staticContext: StaticContext = {}; + + render() { + return ( + +
+ +

Route with statusCode 404

+ +
+
+
+ ); + } +} + +export default StaticRouterExample; diff --git a/types/react-router/tsconfig.json b/types/react-router/tsconfig.json index f3a225ece1..4790f0b53d 100644 --- a/types/react-router/tsconfig.json +++ b/types/react-router/tsconfig.json @@ -29,6 +29,7 @@ "test/Recursive.tsx", "test/RouteConfig.tsx", "test/Sidebar.tsx", + "test/StaticRouter.tsx", "test/Switch.tsx", "test/WithRouter.tsx" ]