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

Prevented Link Double Render in React #79

Merged
merged 4 commits into from
Aug 30, 2016
Merged

Prevented Link Double Render in React #79

merged 4 commits into from
Aug 30, 2016

Conversation

grahammendick
Copy link
Owner

The forceUpdate in the onNavigate handler caused an unnecessary re-render. Immediately after a Link component renders the onNavigate handler fires which causes a re-render even though nothing's changed.

ReactDOM.render is synchronous only if mounting a brand new component. When ReactDOM.render is async then the unnecessary re-render doesn't happen because the onNavigate fires before the first render and so React squashes the two render requests into one.

Changed the Link components to store the state context url in React state each time they render. Then the onNavigate handler only re-renders if the the state context url has changed since previous render.

The Back Link stores the crumb as well as the url in state because the crumb can change even when the context url doesn't (with a custom truncate crumb trail). Accessing the crumb is cheap because it's not evaluated each time, it's just take from the cached crumbs.

Didn't implement this in knockout or angular because the Links update so often it didn't seem worth it.

Previously the Links were rendering twice. Once when they render and
then again because the onNavigation listener fired. This only happened
on initial navigation (not refresh) because that's synchronous -
subsequent React renders are async so the onNavigate fires before the
render and React only renders once.
The Back Link still renders twice because it's possible that navigating
the same Url can change the Back Links if there's a custom truncate url
that allows repeated states
The lazy update shouldn't know about the internal state of components.
Think Back Link needs different state anyway, so better to use
forceUpdate. The onNavigate listener isn't added if lazy so no need to
track state for a lazy update
The Back Link doesn't need to re-render if the crumb hasn't changed.
But, back link might have active props in future and these could change
even if crumb hasn't (could navigate to same url as crumb but the crumb
trail could still have that crumb in it if there's custom truncation).
Including the stateContext url in the check caters for this and matches
with Refresh and Navigation Links
@grahammendick grahammendick merged commit bd66b50 into master Aug 30, 2016
@grahammendick grahammendick deleted the skiprerender branch August 30, 2016 14:20
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant