Skip to content

Instantly share code, notes, and snippets.

@download13
Created December 4, 2015 18:27
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save download13/af727f8fdb071b8e1a4d to your computer and use it in GitHub Desktop.
Save download13/af727f8fdb071b8e1a4d to your computer and use it in GitHub Desktop.
import element from 'virtual-element'; // Using deku
// import React from 'react'; // Switch to this if you are using React
import pathToRegexp from 'path-to-regexp';
export function renderRoutes(path, routes) {
return Object.keys(routes).map(routePath => {
let paramsInfo = [];
let re = pathToRegexp(routePath, paramsInfo);
let paramNames = paramsInfo.map(p => p.name);
let match = re.exec(path);
if(match) {
let Component = routes[routePath];
let params = pairsToObj(zip(paramNames, match.slice(1)));
return <Component params={params} />
} else {
return <noscript/>;
}
});
}
function zip(a1, a2) {
return a1.map((v, i) => [v, a2[i]]);
}
function pairsToObj(arr) {
let o = {};
arr.forEach(pair => {
o[pair[0]] = pair[1]
});
return o;
}
@xcatliu
Copy link

xcatliu commented Dec 9, 2015

Linked to redux-simple-router's issue: reactjs/react-router-redux#65 (comment)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment