简体   繁体   English

reactjs中弹出新消息时如何将div滚动到底部零

[英]how to scroll div to bottom zero when new message pop ups in react js

I am building a chat application.我正在构建一个聊天应用程序。 Although everything is working perfectly fine.尽管一切正常。 But the problem here is when user types new msg it goes below the chat display area and user have to manually scroll down to see the message.但这里的问题是,当用户键入新消息时,它会出现在聊天显示区域下方,用户必须手动向下滚动才能看到消息。 I want to know how can I resolve this issue in my app.我想知道如何在我的应用程序中解决此问题。 so that when message is submitted the it appears immediately in the chat area.这样当消息提交时,它会立即出现在聊天区域中。

Here is my code.这是我的代码。

chatRoomPage.js聊天室页面.js

<Grid  item xs={12}>
                    <div style={{maxHeight: '500px', height: '500px',
                        position: 'fixed',width:'100%',
                        overflow: 'auto'}}>
                    {receivedMessage && receivedMessage.content &&
                    receivedMessage.content.map((item,index) => {
                        if(item.senderId === item.vendorId) {
                            return (
                            <ChatThread key={index} image={item.senderImage} right={true} content={item.content}/>
                            )
                        }else {
                        return (
                            <ChatThread key={index}  image={item.reciverImage} content={item.content}/>
                        )}
                    }) }
                    </div>
                    <br/>
                    <br/>
                    <br/>
                    <br/>
                    <br/>
                    <br/>
                </Grid>

This is the ui这是ui

图像ui

One of the things you can do is to create a ref on the div which renders the chat messages.您可以做的一件事是在 div 上创建一个 ref 来呈现聊天消息。 Upon submitting a chat message just do ref.current.scrollTop = ref.current.scrollHeight;提交聊天消息后,只需执行ref.current.scrollTop = ref.current.scrollHeight;

Code Snippet:代码片段:

import React, { useState, useEffect, useRef } from "react";

const MessageContainer = props => {
    const [messages, setMessages] = useState([]);
    const ref = useRef();
    useEffect(() => {
        // api call get messages
        setMessages([...]);
    }, [messages])

    const submitMessage = () => {
        // post message to server
        ref.current.scrollTop = ref.current.scrollHeight;
    }

  return <>
        ... 
        <Grid  item xs={12}>
            <div ref={ref} style={{maxHeight: '500px', height: '500px',
                position: 'fixed',width:'100%',
                overflow: 'auto'}}>
            {receivedMessage && receivedMessage.content &&
            receivedMessage.content.map((item,index) => {
                if(item.senderId === item.vendorId) {
                    return (
                    <ChatThread key={index} image={item.senderImage} right={true} content={item.content}/>
                    )
                }else {
                return (
                    <ChatThread key={index}  image={item.reciverImage} content={item.content}/>
                )}
            }) }
            </div>
            <br/>
            <br/>
            <br/>
            <br/>
            <br/>
            <br/>
        </Grid>
      <form onSubmit={submitMessage}>
        <input type='text'/>
        <button type='submit'>Submit</button>
      </form>
     ...
      </>
};

export default MessageContainer;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM