How to maintain Scroll Position When Adding Content to the Top of a Container in React

一世执手 提交于 2020-07-07 08:35:54

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!