Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix #264 Preact Router is not able to be extended #265

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
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
29 changes: 22 additions & 7 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,16 @@ const subscribers = [];

const EMPTY = {};

/**
* Set customHistory variable
* because customHistory is read-only when exports
*
* @param {*} newCustomHistory history object
*/
function setCustomHistory(newCustomHistory) {
customHistory = newCustomHistory;
}

function isPreactElement(node) {
return node.__preactattr_!=null || typeof Symbol!=='undefined' && node[Symbol.for('preactattr')]!=null;
}
Expand Down Expand Up @@ -148,15 +158,18 @@ function initEventListeners() {
class Router extends Component {
constructor(props) {
super(props);
if (props.history) {
customHistory = props.history;
}
/** this condition allows to modify the constructor */
if (Object.getPrototypeOf(this.constructor).name === 'Component') {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This breaks when minified.

if (props.history) {
setCustomHistory(props.history);
}

this.state = {
url: props.url || getCurrentUrl()
};
this.state = {
url: props.url || getCurrentUrl()
};

initEventListeners();
initEventListeners();
}
}

shouldComponentUpdate(props) {
Expand Down Expand Up @@ -265,4 +278,6 @@ Router.Route = Route;
Router.Link = Link;

export { subscribers, getCurrentUrl, route, Router, Route, Link };
/** allows to modify the Router class */
export { canRoute, delegateLinkHandler, routeTo, customHistory, setCustomHistory };
export default Router;