Skip to content

Commit

Permalink
Merge pull request #1654 from SUI-Components/feat/avoid-router-rerender
Browse files Browse the repository at this point in the history
feat(packages/sui-react-router): avoid extra render during first load
  • Loading branch information
andresz1 authored Nov 6, 2023
2 parents 51b4284 + 14705d8 commit 029a344
Showing 1 changed file with 14 additions and 2 deletions.
16 changes: 14 additions & 2 deletions packages/sui-react-router/src/Router.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,15 +66,27 @@ const Router = ({

useEffect(() => {
let prevState = {}
let isSkipped = !!matchContext

const handleTransition = (err, nextState) => {
if (err) {
if (onError) return onError(err)
throw err
}

// 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 (isSkipped) {
isSkipped = false
prevState = nextState
return
}

if (prevState === nextState) {
return
}

prevState = nextState

const {components, params, location, routes} = nextState
Expand Down

0 comments on commit 029a344

Please sign in to comment.