【问题标题】:React auto scroll to bottom on a chat container在聊天容器上反应自动滚动到底部
【发布时间】:2019-03-12 09:47:30
【问题描述】:

我正在尝试使用 react 构建一个聊天页面。我显然遇到了一个问题,聊天气泡容器不会在 componentDidMount 和 Update 上自动向下滚动到底部。

我正在查看之前的问答,但找不到任何合适的解决方案。

这是组件。

// renders the text form and all the messages

import React, { Component } from 'react';
import { convo } from '../../data/convo';
import SingleMessage from '../singleMessage/singleMessage';
import StyledForm from './styles';
import moment from 'moment';

class MessageRoom extends Component {

//convo contains the messages
    state = {
        convo,
        message: ''
    };

    handleChange = e => {
        const message = e.target.value;
        this.setState({ message });
    };

    onSubmit = e => {
        e.preventDefault();
        if (this.state.message) {
            const text = {
                message: this.state.message,
                owner: 0,
                date: moment()
            };

            this.setState({ convo: [...this.state.convo, text], message: '' });
        }
    };

    render() {
        return (
            <StyledForm>
                <div className="messages">
                    {this.state.convo.map(text => (
                        <SingleMessage text={text} key={text.date} />
                    ))}
                </div>
                <div>
                    <form
                        onSubmit={e => {
                            this.onSubmit(e);
                        }}
                    >
                        <input
                            type="text"
                            placeholder="Type a message"
                            value={this.state.message}
                            onChange={this.handleChange}
                        />
                        <button type="submit"> Send </button>
                    </form>
                </div>
            </StyledForm>
        );
    }
}

export default MessageRoom;

所以请帮助兄弟!

【问题讨论】:

  • 在你的容器上放一个ref,然后滚动到componentDidMount的底部。 this.ref.current.scrollTop = this.ref.current.scrollHeight;
  • 你能发个sn-p什么的吗!
  • 电流代表什么?

标签: javascript reactjs


【解决方案1】:

// renders the text form and all the messages

import React, { Component } from 'react';
import { convo } from '../../data/convo';
import SingleMessage from '../singleMessage/singleMessage';
import StyledForm from './styles';
import moment from 'moment';

class MessageRoom extends Component {
	constructor() {
		super();
		this.state = {
			convo,
			message: ''
		};

		this.mesRef = React.createRef();
	}

	componentDidMount() {
		this.scrollToBottom();
	}

	scrollToBottom = () => {
		this.mesRef.current.scrollTop = this.mesRef.current.scrollHeight;
	};

	handleChange = e => {
		const message = e.target.value;
		this.setState({ message });
	};

	onSubmit = e => {
		e.preventDefault();
		if (this.state.message) {
			const text = {
				message: this.state.message,
				owner: 0,
				date: moment()
			};

			this.setState(
				{ convo: [...this.state.convo, text], message: '' },
				() => {
					this.scrollToBottom();
				}
			);
		}
	};

	render() {
		return (
			<StyledForm>
				<div className="messages" ref={this.mesRef}>
					{this.state.convo.map(text => (
						<SingleMessage text={text} key={text.date} />
					))}
				</div>
				<div>
					<form
						onSubmit={e => {
							this.onSubmit(e);
						}}
					>
						<input
							type="text"
							placeholder="Type a message"
							value={this.state.message}
							onChange={this.handleChange}
						/>
						<button type="submit"> Send </button>
					</form>
				</div>
			</StyledForm>
		);
	}
}

export default MessageRoom;

将代码更新为新的 ref 用法。

【讨论】:

【解决方案2】:

有一个非常简单的方法来实现它一些 css 技巧

将 Html 包装到父 div 中以获取消息

<div className="message-holder">
    <div className="message"> //state all message
       ...text goes here
    </div>
</div>

<style>
.message-holder{
   position:absolute;
   bottom:0;
   //if required overflow to scroll add below css
  overflow-y:scroll
  max-height: //whatever is required
}
.message{
   //css style goes here
}
</style>

有问题联系我

【讨论】:

    猜你喜欢
    • 2018-09-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-12-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多