From 46d4034ff4984bc9748c40a7a34e24e02e596992 Mon Sep 17 00:00:00 2001 From: Thomas Roch Date: Thu, 26 Nov 2015 18:20:26 +0000 Subject: [PATCH] fix: register node listener in componentDidMount to prevent server rendering issues Relates to #11 --- modules/routeNode.js | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/modules/routeNode.js b/modules/routeNode.js index 5c5d183..76ea119 100644 --- a/modules/routeNode.js +++ b/modules/routeNode.js @@ -10,19 +10,21 @@ function routeNode(nodeName, register = false) { constructor(props, context) { super(props, context); this.router = context.router; - this.nodeListener = (toState, fromState) => this.setState({ previousRoute: fromState, route: toState }); - if (!this.router.registeredPlugins.LISTENERS) { - throw new Error('[react-router5][RouteNode] missing plugin router5-listeners.'); - } this.state = { previousRoute: null, route: this.router.getState() }; - this.router.addNodeListener(nodeName, this.nodeListener); } componentDidMount() { if (register) this.router.registerComponent(nodeName, this.refs.wrappedInstance); + + if (!this.router.registeredPlugins.LISTENERS) { + throw new Error('[react-router5][RouteNode] missing plugin router5-listeners.'); + } + + this.nodeListener = (toState, fromState) => this.setState({ previousRoute: fromState, route: toState }); + this.router.addNodeListener(nodeName, this.nodeListener); } componentWillUnmout() {