Set scroll location in Server Component?
Answered
Segugio Italiano posted this in #help-forum
Segugio ItalianoOP
Hello,
Is it possible to set the scroll location in a server component? i.e. every page should always set the scroll location to say x = 100. It seems if its not, then I should switch part of my component to use client and access the dom element and use a scrollIntoView call?
Is it possible to set the scroll location in a server component? i.e. every page should always set the scroll location to say x = 100. It seems if its not, then I should switch part of my component to use client and access the dom element and use a scrollIntoView call?
Answered by averydelusionalperson
as long as IK there is no way you can set scroll location in server component, since yKnow it's server, and the code has not been sent to the browser yet. So you can't access browser APIS.
You can only scroll in client page.
Hope it helps.
You can only scroll in client page.
Hope it helps.
14 Replies
as long as IK there is no way you can set scroll location in server component, since yKnow it's server, and the code has not been sent to the browser yet. So you can't access browser APIS.
You can only scroll in client page.
Hope it helps.
You can only scroll in client page.
Hope it helps.
Answer
American black bear
'use client';
import {useEffect} from 'react';
export default function ResetScroll() {
useEffect(() => {
window.scroll(0, 0)
}, [])
return null
}if you add this client component into a server component (
<ResetScroll/>) it'll reset the scrollidk if this is the best way to do it, but it works for me
@American black bear ts
'use client';
import {useEffect} from 'react';
export default function ResetScroll() {
useEffect(() => {
window.scroll(0, 0)
}, [])
return null
}
if you add this client component into a server component (`<ResetScroll/>`) it'll reset the scroll
idk if this is the best way to do it, but it works for me
Segugio ItalianoOP
Yeah I am trying something like this except need to do it to a specific element.
What do you mean by specific element?
Segugio ItalianoOP
I have an element that can scroll left to right, I want to set its scroll position to the max right position.
get the element's width, and scroll it to right to it's width? IDK I'm just guessing, and this is not related to Next.js. You can ask Chatgpt, it is pretty good at these things.
Segugio ItalianoOP
Good idea, I was really trying to understand here if a nextJs server component could do it 🙂
No, Server component can't do anything that's related to client, hope it helps. I think this thread should be closed if you have no more doubts, what do you say?
Segugio ItalianoOP
'use client';
import { useEffect, useRef } from 'react';
export default function SetScroll({ children }: { children: React.ReactNode }) {
const divRef = useRef<HTMLDivElement>(null);
useEffect(() => {
if (divRef && divRef.current) {
const div = divRef.current;
const actionList = div.querySelectorAll('div');
console.log(actionList);
const nodeToShow = actionList[actionList.length - 1];
nodeToShow.scrollIntoView({
behavior: 'smooth',
block: 'nearest',
inline: 'center',
});
}
}, []);
return (
<div ref={divRef} className="flex p-1 space-x-4 w-max">
{children}
</div>
);
}
import { useEffect, useRef } from 'react';
export default function SetScroll({ children }: { children: React.ReactNode }) {
const divRef = useRef<HTMLDivElement>(null);
useEffect(() => {
if (divRef && divRef.current) {
const div = divRef.current;
const actionList = div.querySelectorAll('div');
console.log(actionList);
const nodeToShow = actionList[actionList.length - 1];
nodeToShow.scrollIntoView({
behavior: 'smooth',
block: 'nearest',
inline: 'center',
});
}
}, []);
return (
<div ref={divRef} className="flex p-1 space-x-4 w-max">
{children}
</div>
);
}
Above was my solution as a non-server component.
@averydelusionalperson No, Server component can't do anything that's related to client, hope it helps. I think this thread should be closed if you have no more doubts, what do you say?
Segugio ItalianoOP
I marked your answer as the solution 🙂
@Segugio Italiano I marked your answer as the solution 🙂
You could have marked your own message, since you found the solution. But I ain't complaining
@averydelusionalperson You could have marked your own message, since you found the solution. ~~But I ain't complaining~~
Segugio ItalianoOP
Well, your answer was correct, I was already working on my client solution, just curious if there was a server one too.