Skip to content

Commit

Permalink
test(packages/sui-react-router): avoid extra render during first load
Browse files Browse the repository at this point in the history
  • Loading branch information
Andrés Alvarez authored and Andrés Alvarez committed Nov 2, 2023
1 parent 51b4284 commit abe3d62
Showing 1 changed file with 12 additions and 3 deletions.
15 changes: 12 additions & 3 deletions packages/sui-react-router/src/Router.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,16 +65,25 @@ 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)
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 (!prevState) {
prevState = nextState
return
}

if (prevState === nextState) {
return
}

prevState = nextState

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

0 comments on commit abe3d62

Please sign in to comment.