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: apply components to mdx #638

Closed
wants to merge 1 commit into from
Closed

Conversation

promer94
Copy link
Collaborator

@promer94 promer94 commented Aug 5, 2022

close #637

@changeset-bot
Copy link

changeset-bot bot commented Aug 5, 2022

🦋 Changeset detected

Latest commit: 85689cc

The changes in this PR will be included in the next version bump.

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@vercel
Copy link

vercel bot commented Aug 5, 2022

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Updated
nextra-theme-docs-dev ✅ Ready (Inspect) Visit Preview Aug 5, 2022 at 3:50AM (UTC)
1 Ignored Deployment
Name Status Preview Updated
nextra ⬜️ Ignored (Inspect) Aug 5, 2022 at 3:50AM (UTC)

Comment on lines -206 to -222
function Content(props) {
function Page(props) {
return (
<__nextra_SSGContext__.Provider value={props}>
<MDXContent />
{__nextra_withLayout__(
MDXContent,
{
titleText: typeof titleText === 'string' ? titleText : undefined,
...__nextra_pageOpts__
},
${layoutConfig ? '__nextra_layoutConfig__' : 'null'},
)}
</__nextra_SSGContext__.Provider>
)
}

export default __nextra_withLayout__(
${JSON.stringify(pageNextRoute)},
Content,
{
titleText: typeof titleText === 'string' ? titleText : undefined,
...__nextra_pageOpts__
},
${layoutConfig ? '__nextra_layoutConfig__' : 'null'},
)`.trimStart()
Copy link
Owner

Choose a reason for hiding this comment

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

Let me explain a bit on my previous change. _app.js renders the exported component from each page like render(<Page/>). And because function Page(props) { is a different function on every different page, when a client navigation happens, React re-renders the render(<Page2/>). Because <Page/> and <Page2/> have different types (Page !== Page2), React unmounts the <Page/> component and mounts the <Page2/>.

Because the layout stuff (navbar, sidebar, etc.) are all inside <Page/>, the states there will be reset too. That's why we had the getLayout() stuff.

And to avoid that, I refactored the withLayout HoC to always return the same component Layout, but use route to get the correct content to render inside. This way the layout will preserve the state and always be stable.

Copy link
Owner

Choose a reason for hiding this comment

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

the states there will be reset too

There are two states, one is the React state (useState) and another one is the DOM state (scroll positions):

CleanShot 2022-08-05 at 11 40 39

@promer94 promer94 closed this Aug 5, 2022
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.

None yet

2 participants