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

fix(v2): useTOC hooks should not be called in each nested children #1959

Merged
merged 3 commits into from
Nov 11, 2019

Conversation

endiliey
Copy link
Contributor

Motivation

TOC hooks is called inside each nested children previously. See gif below on how many getActiveHeader which leads to document query calls.

See recursive headings

function Headings({headings, isChild}) {
useTOCHighlight(LINK_CLASS_NAME, ACTIVE_LINK_CLASS_NAME, TOP_OFFSET);
if (!headings.length) return null;
return (
<ul className={isChild ? '' : 'contents contents__left-border'}>
{headings.map(heading => (
<li key={heading.id}>
<a href={`#${heading.id}`} className={LINK_CLASS_NAME}>
{heading.value}
</a>
<Headings isChild headings={heading.children} />
</li>
))}
</ul>
);
}

We should also do better in future by doing some throttling, but that's out of scope of this PR.
We can also split RightTOC component as another @theme/RightTOC component so its swizzle-able if needed (but better in future PR so that lerna autogenerated changelog can pick it better)

before call

Have you read the Contributing Guidelines on pull requests?

yes

Test Plan

  • Netlify
    right toc still highlighted on scroll
  • Less number of getActiveHeader calls
    after

@endiliey endiliey added the pr: bug fix This PR fixes a bug in a past release. label Nov 11, 2019
@facebook-github-bot facebook-github-bot added the CLA Signed Signed Facebook CLA label Nov 11, 2019
@docusaurus-bot
Copy link
Contributor

Deploy preview for docusaurus-2 ready!

Built with commit ed626cf

https://deploy-preview-1959--docusaurus-2.netlify.com

@docusaurus-bot
Copy link
Contributor

Deploy preview for docusaurus-preview ready!

Built with commit ed626cf

https://deploy-preview-1959--docusaurus-preview.netlify.com

@docusaurus-bot
Copy link
Contributor

Deploy preview for docusaurus-2 ready!

Built with commit 81be1ea

https://deploy-preview-1959--docusaurus-2.netlify.com

@docusaurus-bot
Copy link
Contributor

Deploy preview for docusaurus-preview ready!

Built with commit 81be1ea

https://deploy-preview-1959--docusaurus-preview.netlify.com

Copy link
Contributor

@yangshun yangshun left a comment

Choose a reason for hiding this comment

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

Good catch 👍

@yangshun yangshun merged commit 5e445a0 into master Nov 11, 2019
@yangshun yangshun deleted the endi/tochooks branch November 11, 2019 12:56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
CLA Signed Signed Facebook CLA pr: bug fix This PR fixes a bug in a past release.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants