问题
I am trying to create messaging app in react. When ever the scroll is at the top, it dispatch an action to fetch older messages but the scroll still stays at top even after the messages are fetched so it keeps on firing the dispatch action to get more messages. Does any one know how to main the scroll position when adding content to the top of parent containers's children so that scroll position doesn't stay at top and stops firing request.
<Fragment>
{messagesLoading && messages.length > 0 && <NoramlSpinner/>}
<li id='first' ref={lastPostElRef}>
<div className='mb-1 left-msg' >
</div>
</li>
{messages.length > 0 &&
messages.map((message, i)=>{
if(profile.user._id !== message.sender._id ){
return <LeftMessage message={message} key={message._id}/>
}
else{
return <RightMessages message={message} key={message._id}/>
}
})}
</Fragment>
来源:https://stackoverflow.com/questions/61346697/how-to-maintain-scroll-position-when-adding-content-to-the-top-of-a-container-in