From 78a6aef7ffcb95a392a87fe601c4ec308e6532b0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9s=20Alvarez?= Date: Thu, 2 Nov 2023 09:27:38 +0100 Subject: [PATCH 1/3] feat(packages/sui-react-router): avoid extra rerender during first load --- packages/sui-react-router/src/Router.js | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/packages/sui-react-router/src/Router.js b/packages/sui-react-router/src/Router.js index 6ef428f3b..d0a055e2a 100644 --- a/packages/sui-react-router/src/Router.js +++ b/packages/sui-react-router/src/Router.js @@ -65,7 +65,7 @@ const Router = ({ const [state, setState] = useState({router, params, components}) useEffect(() => { - let prevState = {} + let prevState = null const handleTransition = (err, nextState) => { if (err) { if (onError) return onError(err) @@ -73,8 +73,17 @@ const Router = ({ } // avoid not needed re-renders of the state if the prevState and the nextState - // are the same reference - if (prevState === nextState) return + // are the same reference or if the first render + + if (!prevState) { + prevState = nextState + return + } + + if (prevState === nextState) { + return + } + prevState = nextState const {components, params, location, routes} = nextState From fdce67ff577986bfae3ac9810d022228630eeed5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9s=20Alvarez?= Date: Thu, 2 Nov 2023 12:54:13 +0100 Subject: [PATCH 2/3] feat(packages/sui-react-router): update skip strategy --- packages/sui-react-router/src/Router.js | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/packages/sui-react-router/src/Router.js b/packages/sui-react-router/src/Router.js index d0a055e2a..47c5b7cca 100644 --- a/packages/sui-react-router/src/Router.js +++ b/packages/sui-react-router/src/Router.js @@ -65,7 +65,9 @@ const Router = ({ const [state, setState] = useState({router, params, components}) useEffect(() => { - let prevState = null + let prevState = {} + let isSkipped = !!matchContext + const handleTransition = (err, nextState) => { if (err) { if (onError) return onError(err) @@ -75,8 +77,8 @@ const Router = ({ // avoid not needed re-renders of the state if the prevState and the nextState // are the same reference or if the first render - if (!prevState) { - prevState = nextState + if (isSkipped) { + isSkipped = false return } From 14705d87479e59b74c57c3d64ad87e377ad746ab Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9s=20Alvarez?= Date: Thu, 2 Nov 2023 13:00:35 +0100 Subject: [PATCH 3/3] feat(packages/sui-react-router): small improvement --- packages/sui-react-router/src/Router.js | 1 + 1 file changed, 1 insertion(+) diff --git a/packages/sui-react-router/src/Router.js b/packages/sui-react-router/src/Router.js index 47c5b7cca..671b1451e 100644 --- a/packages/sui-react-router/src/Router.js +++ b/packages/sui-react-router/src/Router.js @@ -79,6 +79,7 @@ const Router = ({ if (isSkipped) { isSkipped = false + prevState = nextState return }