Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(packages/sui-react-router): avoid extra render during first load #1654

Merged
merged 3 commits into from
Nov 6, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If matchContext is provided that means that we can avoid the setState being execute since the router properties are going to be the same


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
Loading