10 examples of 'npm react router dom' in JavaScript

Every line of 'npm react router dom' code snippets is scanned for vulnerabilities by our powerful machine learning engine that combs millions of open source libraries, ensuring your JavaScript code is secure.

All examples are scanned by Snyk Code

By copying the Snyk Code Snippets you agree to
this disclaimer
25function mountWithRouter (node) {
26
27 // Instantiate router context
28 const router = {
29 history: new BrowserRouter().history,
30 route: {
31 location: {},
32 match: {},
33 },
34 }
35
36 const createContext = () => ({
37 context: { router, t: () => {} },
38 childContextTypes: { router: shape({}), t: () => {} },
39 })
40
41 return mount(node, createContext())
42}
Important

Use secure code every time

Secure your code as it's written. Use Snyk Code to scan source code in minutes – no build needed – and fix issues immediately. Enable Snyk Code

8export function Router({children, ...props}) {
9 assert(props.location, 'Router "location" property is missing')
10
11 const routes = useMemo(() => addRoutes(children), [])
12 return renderMatch(routes, props)
13}
10export default function withNamedRouter(ChildComponent) {
11 return function NamedRouter(props) {
12 const routerValue = useContext(RouterContext)
13 const namedRoutes = useContext(RouterConfigContext)
14 const history = useMemo(() => namedHistory(routerValue.history, namedRoutes), [routerValue])
15 return (
16 <ChildComponent
17 {...props}
18 history={history}
19 location={routerValue.location}
20 match={routerValue.match}
21 />
22 )
23 }
24}
11function withRouter(Component) {
12 const displayName = `withRouter(${Component.displayName || Component.name})`;
13 const C = props => {
14 const { wrappedComponentRef, ...remainingProps } = props;
15
16 return (
17 <RouterContext.Consumer>
18 {context => {
19 invariant(
20 context,
21 `You should not use <${displayName} /> outside a <Router>`
22 );
23 return (
24 <Component
25 {...remainingProps}
26 {...context}
27 ref={wrappedComponentRef}
28 />
29 );
30 }}
31 </RouterContext.Consumer>
32 );
33 };
34
35 C.displayName = displayName;
36 C.WrappedComponent = Component;
37
38 if (__DEV__) {
39 C.propTypes = {
40 wrappedComponentRef: PropTypes.oneOfType([
41 PropTypes.string,
42 PropTypes.func,
43 PropTypes.object
44 ])
45 };
46 }
47
48 return hoistStatics(C, Component);
49}
178ReduxibleRouter.prototype.getRouterWithDevTools = function getRouterWithDevTools() {
179 var DevTools = this.devTools;
180 return _react2.default.createElement(
181 'div',
182 null,
183 this.getRouter(),
184 ' ',
185 _react2.default.createElement(DevTools, null)
186 );
187};
17export function createRouteFromReactElement(element) {
18 var type = element.type;
19 var route = createRoute(type.defaultProps, element.props);
20
21 if (route.children) {
22 var childRoutes = createRoutesFromReactChildren(route.children, route);
23
24 if (childRoutes.length) route.childRoutes = childRoutes;
25
26 delete route.children;
27 }
28
29 return route;
30}
18export function renderWithReduxStore( reactElement, domContainer, reduxStore ) {
19 const domContainerNode =
20 'string' === typeof domContainer ? document.getElementById( domContainer ) : domContainer;
21
22 return ReactDom.render(
23 <ReduxProvider store={ reduxStore }>
24 <MomentProvider>{ reactElement }</MomentProvider>
25 </ReduxProvider>,
26 domContainerNode
27 );
28}
17render() {
18 const {children} = this.props;
19 // No children means nothing to render.
20 if (!children) return null;
21
22 // That makes nested routes working.
23 const propsForChildren = {...this.props};
24 delete propsForChildren.children;
25
26 // Delete route to prevent overwrite of correct value.
27 delete propsForChildren.route;
28
29 return React.cloneElement(children, propsForChildren);
30}
26RouterRender(props) {
27 switch (this.props.store.state) {
28 case 'loading': {
29 return ('Loading...');
30 }
31 case 'error': {
32 return ('Error');
33 }
34 case 'ready': {
35 return (
36 <Main {...this.props} {...props}/>
37 );
38 }
39 }
40 return null;
41}
48public render() {
49 return this._resolveRoute();
50}

Related snippets