-
-
Notifications
You must be signed in to change notification settings - Fork 145
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
Scroll on first render of statically generated page with nested child component initialising query state (remote only) #561
Comments
Could you please give an example of the contents of What does the URL look like, is there a fragment section? (eg: |
function ProductPricingSectionUnsafe({ pricing }: ExampleProps ) {
const { data: currencyOptionData } = api.pricing.getCurrencyOptions.useQuery({
pricingId: pricing.id,
})
const currencyOptions =
currencyOptionData?.map(({ currencyCode }) => ({
value: currencyCode,
label: currencyCode,
})) ?? []
// Problem area
const [selectedCurrency, setSelectedCurrency] = useQueryState(
'currency',
parseAsString.withDefault(currencyOptions?.[0]?.value ?? ''),
)
return (
<Section
title="Pricing"
contentRight={
<div className="flex items-center gap-3">
<Combobox
label="Currency"
buttonLeftIcon={CreditCardIcon}
options={currencyOptions}
value={selectedCurrency}
onChange={(v) => setSelectedCurrency(v as CurrencyCode)}
searchPlaceholder="Enter a currency"
className="w-32 min-w-fit"
/>
</div>
}
>
<ScrollArea>
<div className="flex w-full gap-3 lg:gap-5">
{tiers.map((tier) => (
<PricingTierCard
key={tier.id}
tier={tier}
isCustomPricing={tier.isCustomPricing}
chargeableUnit={chargeableUnit}
chargeableUsagePeriod={chargeableUsagePeriod}
freeTrial={freeTrial}
preferredCurrency={selectedCurrency as CurrencyCode}
/>
))}
</div>
<ScrollBar orientation="horizontal" />
</ScrollArea>
</Section>
)
}
export function ProductPricingSection(props: ProductPricingSectionProps) {
const isReady = useRouterReady()
return isReady ? <ProductPricingSectionUnsafe {...props} /> : null
} Url looks like the following - /[id]?currency=GBP (no fragment) Just wondering now if there is some issue with the fact we will have an undefined default prior to having the options available? |
Yes from the look of your example, having the default being populated by an external API call might be the source of the problem. On initial (post-hydration) render, the default will be an empty string (if no search param is present). Then once your API call resolves, the default value changes, and this is what could cause your view to scroll to the new position. What if you try mounting the scroll view only after the API call resolves? Does it also solve the scroll problem? |
This looks to be solved - thanks so much for the speedy response |
Context
What's your version of
nuqs
?"nuqs": "^1.17.4"
Next.js information (obtained by running
next info
):Are you using:
basePath
option in your Next.js configwindowHistorySupport
flag in your Next.js configDescription
On pages which utilise SSG and which include a child component which calls
useQueryState
the page is always scrolled on first render.useQueryState
does respect thescroll
option for subsequent state updates. This issue only occurs upon deployment (i.e. not locally). I'm assuming that SSG is more like sever-side rendering in development.The problematic child component is wrapped in the following (to solve hydration issues as noted in another issue.
Note: This issue is solved by moving the
useQueryState
initialisation to the page level. This is fine but a little suboptimal as state and state setters now need to be passed to children.Reproduction
Example: Steps to reproduce the behavior:
useQueryState
The text was updated successfully, but these errors were encountered: