File tree Expand file tree Collapse file tree 1 file changed +11
-2
lines changed
app/src/components/bottom Expand file tree Collapse file tree 1 file changed +11
-2
lines changed Original file line number Diff line number Diff line change 1
1
import React from 'react' ;
2
- import { useState } from 'react' ;
2
+ import { useState , useRef , useEffect } from 'react' ;
3
3
import { useSelector } from 'react-redux' ;
4
4
import { RootState } from '../../redux/store' ;
5
5
import { emitEvent } from '../../helperFunctions/socket' ;
@@ -89,13 +89,22 @@ const Chatroom = (props): JSX.Element => {
89
89
} ) ;
90
90
} ;
91
91
92
+ const containerRef = useRef < HTMLDivElement > ( null ) ;
93
+
94
+ // Scroll to the bottom of the container whenever new messages are added
95
+ useEffect ( ( ) => {
96
+ if ( containerRef . current ) {
97
+ containerRef . current . scrollTop = containerRef . current . scrollHeight ;
98
+ }
99
+ } , [ messages ] ) ;
100
+
92
101
return (
93
102
< div
94
103
className = "livechat-panel"
95
104
style = { { paddingLeft : '10px' , width : '100%' , height : '100%' } }
96
105
>
97
106
< div style = { { justifyContent : 'center' , display : 'flex' , height : '80%' } } >
98
- < div id = "message-container" style = { wrapperStyles } >
107
+ < div id = "message-container" style = { wrapperStyles } ref = { containerRef } >
99
108
{ renderMessages ( ) }
100
109
</ div >
101
110
</ div >
You can’t perform that action at this time.
0 commit comments