From 70d8a3cd59ec3ba6e946b76c5cfad27980d2e6a2 Mon Sep 17 00:00:00 2001 From: Michael Ridgway Date: Tue, 22 Mar 2016 16:39:50 -0700 Subject: [PATCH] Only store the currentRoute and use the immutable route for checking shouldComponentUpdate --- .../lib/createNavLinkComponent.js | 48 ++++++++++--------- 1 file changed, 26 insertions(+), 22 deletions(-) diff --git a/packages/fluxible-router/lib/createNavLinkComponent.js b/packages/fluxible-router/lib/createNavLinkComponent.js index 93be43b3..4048b648 100644 --- a/packages/fluxible-router/lib/createNavLinkComponent.js +++ b/packages/fluxible-router/lib/createNavLinkComponent.js @@ -83,10 +83,12 @@ module.exports = function createNavLinkComponent (overwriteSpec) { } }, shouldComponentUpdate: function (nextProps, nextState) { - return (this.state.isActive !== nextState.isActive || this.receivedNewProps); + return ( + nextProps !== this.props || + this.state.currentRoute !== nextState.currentRoute + ); }, componentWillReceiveProps: function (nextProps) { - this.receivedNewProps = true; this.setState(this._getState(nextProps)); }, _onRouteStoreChange: function () { @@ -96,10 +98,8 @@ module.exports = function createNavLinkComponent (overwriteSpec) { }, _getState: function (props) { var routeStore = this.context.getStore(RouteStore); - var href = this._getHrefFromProps(props); return { - href: href, // derived from props - isActive: routeStore.isActive(href) // derived from href and state + currentRoute: routeStore.getCurrentRoute() }; }, _getHrefFromProps: function (props) { @@ -196,27 +196,29 @@ module.exports = function createNavLinkComponent (overwriteSpec) { this.dispatchNavAction(e); }, render: function () { - var routeStore = this.context.getStore(RouteStore); - var className = this.props.className; + var href = this._getHrefFromProps(this.props); + + var isActive = false; + if (this.props.activeClass || this.props.activeStyle || this.props.activeElement) { + var routeStore = this.context.getStore(RouteStore); + isActive = routeStore.isActive(href); + } + var style = {}; - if (this.props.activeClass || this.props.activeStyle) { - var isActive = routeStore.isActive(this.state.href); - if (isActive) { - if (this.props.activeClass) { - className = className ? (className + ' ') : ''; - className += this.props.activeClass; - } - if (this.props.activeStyle) { - Object.assign(style, this.props.style, this.props.activeStyle); - } + var className = this.props.className; + if (isActive) { + if (this.props.activeClass) { + className = className ? (className + ' ') : ''; + className += this.props.activeClass; + } + if (this.props.activeStyle) { + Object.assign(style, this.props.style, this.props.activeStyle); } } - this.receivedNewProps = false; - var childElement = this.state.isActive ? this.props.activeElement || 'a' : 'a'; var childProps = {}; - if (!(this.state.isActive && this.props.activeElement)) { + if (!(isActive && this.props.activeElement)) { childProps.onClick = this.clickHandler.bind(this); } @@ -225,10 +227,12 @@ module.exports = function createNavLinkComponent (overwriteSpec) { style: style }); - if (!(this.state.isActive && this.props.activeElement)) { - childProps.href = this.state.href; + if (!(isActive && this.props.activeElement)) { + childProps.href = href; } + var childElement = isActive ? this.props.activeElement || 'a' : 'a'; + return React.createElement( childElement, childProps,