scroll to bottom of div in react
scroll to the bottom of a div in react:
div
add an element at the bottom of the .-
set a on the bottom element
ref
. -
when an event occurs, the method
ref
of the object is calledscrollintoview()
.
import {useeffect, useref, usestate} from 'react';
export default functionapp() {
const bottomref = useref(null);
const [messages, setmessages] = usestate([]);
useeffect(() => {
// 👇️ simulate chat messages flowing in
setinterval(
() =>
setmessages(current => [
...current,
'lorem ipsum dolor sit amet consectetur, adipisicing elit. porro, quaerat eum id obcaecati, magnam voluptatum dolorem sunt, omnis sed consectetur necessitatibus blanditiis ipsa? cumque architecto, doloribus mollitia velit non sint!',
]),
600,
);
}, []);
useeffect(() => {
// 👇️ scroll to bottom every time messages change
bottomref.current?.scrollintoview({behavior: 'smooth'});
}, [messages]);
return (
<div><h2>top of the pageh2><div>
{messages.map((message, index) => {
return <pkey={index}>{message}p>;
})}
<divref={bottomref} />div>div>
);
}
this code shows how to scroll to the bottom of the div each time a new chat message flows in.
the first call to useeffect
the hook simulates new messages appearing every 600 milliseconds.
we useref
initialize a ref using the hook.
useref()
the hook can be passed an initial value as an argument. the hook returns a mutable ref object whose .current property is initialized to the passed argument.
note that we have to access
current
the property of the ref object to access the div element on which we set the ref attribute.
when we pass a ref prop to an element, for example
.current
sets the property of the ref object to the corresponding dom node.we added the message state variable as a dependency in the second
useeffect
hook because we want to re-run the code in the hook every time the message changes.
useeffect(() => {
bottomref.current?.scrollintoview({behavior: 'smooth'});
}, [messages]);
we use scrollintoview
the method to scroll to the bottom of the chat message container div element.
behavior
property specifies whether scrolling should be smooth (smooth
) or immediate (auto
).
behavior
the default value of the property is auto
.
every time the message changes, useeffect
the hook is re-run and we call scrollintoview()
the method to scroll to the bottom of the div and display the latest message.
for reprinting, please send an email to 1244347461@qq.com for approval. after obtaining the author's consent, kindly include the source as a link.
article url:
related articles
how to draw a horizontal line in react
publish date:2025/03/11 views:73 category:react
-
在 react 中画一条水平线: 使用 hr / 标签并在其上设置 style 属性。 设置线条的高度,并可选择设置背景颜色和颜色。
how to use buttons as links in react
publish date:2025/03/10 views:140 category:react
-
要将按钮用作 react 中的链接,请将按钮包装在 标记中,如果使用 react 路由器,则将其包装在 link 组件中。 该按钮将被呈现而不是链接,单击它将导致浏览器导航到指定的页面。
using onclick on div elements in react
publish date:2025/03/10 views:121 category:react
-
要在 react 中的 div 元素上设置 `onclick` 监听器: 在 div 上设置 `onclick` 属性。 每次单击 div 时,都会调用我们传递给 prop 的函数。 我们可以通过 event.currenttarget 访问 div。
react: how to upload multiple files using axios
publish date:2025/03/10 views:146 category:react
-
这篇简洁明了的文章向您展示了如何在 axios 的帮助下在 react 中上传多个文件。这项工作并不太庞大,可以通过以下几个简单的步骤完成(您将在第 3 步中看到 javascript 和 typescript 代码片
how to toggle a boolean state in react
publish date:2025/03/10 views:93 category:react
-
在 react 中切换布尔状态:使用 usestate 钩子来跟踪布尔值的状态。 将函数传递给钩子返回的 setstate 函数。根据当前值切换布尔值,例如 setisloading(current => !current)。
creating a back button with react router
publish date:2025/03/10 views:155 category:react
-
使用 react router 创建后退按钮: 将按钮上的 onclick 属性设置为函数。使用 usenavigate() 钩子,例如 const navigate = usenavigate();。调用 navigate() 函数,传递它 -1 - navigate(-1)。
how to create a read more/less button in react
publish date:2025/03/10 views:168 category:react
-
在构建 web 应用程序时,有时我们需要显示一些长文本。在这种情况下,一个聪明的设计是只显示部分文本并添加一个显示更多按钮供用户在需要时展开文本。当文本已展开并完全显示时
insert style tags into react components
publish date:2025/03/10 views:66 category:react
-
要在 react 组件中插入样式标签,请将我们的 css 包装在样式标签中的模板字符串中。 花括号之间的表达式将被评估并添加样式。
how to listen to state changes in react
publish date:2025/03/10 views:179 category:react
-
使用 useeffect 钩子来监听 react 中的状态变化。 我们可以将要跟踪的状态变量添加到挂钩的依赖项数组中,并且每次状态变量更改时都会运行 useeffect 钩子中的逻辑。