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 1 commit
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
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
Loading