react-use/src/createRouter.ts
2018-10-27 10:00:10 +02:00

35 lines
737 B
TypeScript

import * as React from 'react';
export interface RouterProviderProps {
route: string;
fullRoute?: string;
parent?: any;
}
const createRouter = () => {
const context = React.createContext<RouterProviderProps>({
route: '',
});
const Router: React.SFC<RouterProviderProps> = (props) => {
const {route, fullRoute, parent, children} = props;
if (process.env.NODE_ENV !== 'production') {
if (typeof route !== 'string') {
throw new TypeError('Router route must be a string.');
}
}
return React.createElement(context.Provider, {
value: {
fullRoute: fullRoute || route,
route,
parent,
},
children,
});
}
};
export default createRouter;