Skip to content

Commit

Permalink
fix(RouteView): Proper error handling for RouteView
Browse files Browse the repository at this point in the history
When it is not possible to select the correct component to render then RouteView should not render
anything and errors are printed to the console.
  • Loading branch information
LeonardoGentile committed Aug 8, 2017
1 parent f442904 commit 7e65479
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 12 deletions.
2 changes: 0 additions & 2 deletions src/modules/BaseLink.js
Original file line number Diff line number Diff line change
Expand Up @@ -119,8 +119,6 @@ BaseLink.propTypes = {
[storeName]: PropTypes.object,
route: PropTypes.object,
isActive: PropTypes.bool


};

export default BaseLink;
23 changes: 13 additions & 10 deletions src/modules/RouteView.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import {createElement} from 'react';
import {getComponent} from './utils';

/**
* Route component: it should be used inside a routeNode component
* RouteView component: it should be used inside a routeNode component
*
* It select and render the correct component associated with the current route for the given routeNodeName
*
Expand All @@ -13,22 +13,25 @@ function RouteView(props) {

const {route, routeNodeName, routes, ...passThroughProps } = props;

let routeToRender = route;
let currentRoute = route;
let ComponentToRender = null;

if (!routeToRender) {
throw new Error('Route Component requires a route prop');
}

try {
ComponentToRender = getComponent(routeToRender, routeNodeName, routes);
if (!currentRoute) {
throw new Error('RouteView component requires a route prop');
}
const FetchedComponent = getComponent(currentRoute, routeNodeName, routes); // getComponent may throw
// Add `{key: route.meta.id}` to props for a full unmount/mount
ComponentToRender = createElement(FetchedComponent, {...passThroughProps, route});
}
catch (e) {
throw e;
// Do not render and print error to console
console.error(`RouteView: it was not possible to select the correct view for the current route '${currentRoute.name}' having params: `);
// This outputs an object on the browser console you can click through
console.dir(currentRoute.params);
}

// Add ==> {key: route.meta.id}, to props to pass below for a full unmount/mount
return ComponentToRender ? createElement(ComponentToRender, {...passThroughProps, route} ) : null;
return ComponentToRender;
}


Expand Down

0 comments on commit 7e65479

Please sign in to comment.