我正在创建一个松弛克隆应用程序,并且希望当用户添加新消息或当新消息通过它时滚动到消息的底部......当我重新加载频道或使用 enter 提交消息时它可以工作。但是,当我单击按钮时,什么也没有
我已经尝试了几个 npm 包并且移动 REF 没有运气
componentDidUpdate(){
{this.messagesEnd && this.scrollToBottom()}
}
scrollToBottom = ()=> {
this.messagesEnd.scrollIntoView({behavior: 'smooth'})
}
// 在输入时添加发送消息
handleKeyDown = (e) => {
if(e.keyCode === 13){
this.sendMessage()
}
const { message, typingRef, channel, user } = this.state;
if(message){
typingRef
.child(channel.id)
.child(user.uid)
.set(user.displayName)
}else{
typingRef
.child(channel.id)
.child(user.uid)
.remove()
}
sendMessage = () => {
const { getMessagesRef } = this.props;
const { message, channel, user, typingRef } = this.state;
if(message) {
this.setState({loading: true});
getMessagesRef()
.child(channel.id)
.push()
.set(this.createMessage())
.then(()=> {
this.setState({loading: false, message: '', errors: [] });
typingRef
.child(channel.id)
.child(user.uid)
.remove()
})
.catch(err => {
console.error(err);
this.setState({
loading: false,
errors: [...this.state.errors, err ]
})
})
}else{
this.setState({
errors: [...this.state.errors, {message: "Add a message"}]
})
}
}
<Fragment>
<MessagesHeader
channelName = {this.displayChannelName(channel)}
numUsers = {numUsers}
handleSearchChange={this.handleSearchChange}
searchLoading = {searchLoading}
isPrivateChannel = {isPrivateChannel}
handleStar = {this.handleStar}
isChannelStarred = {isChannelStarred}
/>
<Segment>
<Comment.Group className='messages'>
{this.displayMessagesSkeleton(messagesLoading)}
{searchTerm ? this.displayMessages(searchResults) : this.displayMessages(messages) }
{this.displayTypingUsers(typingUsers)}
<div ref={node => this.messagesEnd = node}></div>
</Comment.Group>
</Segment>
<MessageForm
messagesRef ={messagesRef}
currentChannel = {channel}
currentUser = { user }
isPrivateChannel = {isPrivateChannel}
getMessagesRef = {this.getMessagesRef}
/>
</Fragment>