forked from remix-run/react-router
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapplyRouterMiddleware.js
More file actions
30 lines (27 loc) · 897 Bytes
/
applyRouterMiddleware.js
File metadata and controls
30 lines (27 loc) · 897 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
import React, { createElement } from 'react'
import RouterContext from './RouterContext'
export default (...middlewares) => {
const withContext = middlewares.map(m => m.renderRouterContext).filter(f => f)
const withComponent = middlewares.map(m => m.renderRouteComponent).filter(f => f)
const makeCreateElement = (baseCreateElement = createElement) => (
(Component, props) => (
withComponent.reduceRight(
(previous, renderRouteComponent) => (
renderRouteComponent(previous, props)
), baseCreateElement(Component, props)
)
)
)
return (renderProps) => (
withContext.reduceRight(
(previous, renderRouterContext) => (
renderRouterContext(previous, renderProps)
), (
<RouterContext
{...renderProps}
createElement={makeCreateElement(renderProps.createElement)}
/>
)
)
)
}