Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions modules/applyRouterMiddleware.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/**
* This file is subject to the terms and conditions defined in the LICENSE file
* which is found in the in the root directory of React Router source tree.
*
* https://github.com/reactjs/react-router/blob/master/LICENSE.md
*/

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)}
/>
)
)
);
};