14

I use the ListView's scrollToEnd, but it doesn't work, but it worked for scrollTo. What should I do to deal with it.

enter image description here

enter image description here

5
  • I'm not sure this would work since the ListView doesn't have to render all elements when the view is mounted. There's also no way of knowing when the ListView is done with rendering all the elements. AFAIK the scrollToEnd method can be used with a button. Commented Feb 18, 2017 at 12:13
  • Are you getting an error or is it just not doing anything? Commented Feb 19, 2017 at 6:00
  • Also, Android or iOS? Commented Feb 19, 2017 at 6:01
  • @user1221780 no,I have no error about it,It is running on iOS simulator Commented Feb 19, 2017 at 12:18
  • @Eldelshell I have a try,It worked by used with a button,but I want to let the ListView scroll to the end at the beginning Commented Feb 20, 2017 at 6:47

6 Answers 6

20

There is a workaround, wrap it in a setTimeout like so:

componentDidMount() {
  setTimeout(() => {
    this.refs.dateList.scrollToEnd();
  }, 50);
}
Sign up to request clarification or add additional context in comments.

4 Comments

This is valid, but why
Sorry, I don't remember where I got this workaround, but i can say I've spent hours struggling with this bug...
It's bad practice to use setTimeout
It'll just be because rendering takes time. Use onLayout with a bound method which scrolls to end: e.g. scrollToBottom() { this.refs.myView.scrollToEnd() } <List onLayout={this.scrollToBottom}>
16

Try this:

<ListView
    ref={ ( ref ) => this.scrollView = ref }
    onContentSizeChange={ () => {        
        this.scrollView.scrollToEnd( { animated: false } )
    } } >
</ListView>

document

Comments

1

For functional components, the equivalent is using useEffect like so:

useEffect(()=>{
    setTimeout(ref?.current.scrollToEnd());
},[yourArrayData])

Note, you don't need to set a duration for the setTimeout, this is because of Macrotasks in JavaScript, https://javascript.info/event-loop if you want to find out more

2 Comments

Note, with this solution, yourArrayData will cause the useEffect to call if it changes (if it in state) or has perceived to have changed since React does not run a deep check on objects in a dependency array link, this answer is still a viable solution
Yea I place the yourArrayData in the dependency array because it is equivalent to this other solution stackoverflow.com/a/46420586/13710770 Use an empty array if you want this other equivalent solution stackoverflow.com/a/46370418/13710770
0

Rendering takes time so setTimeout() before scrolling to end will work. You can, however, use the onLayout() method.

export default MyComponent extends React.Component {
  constructor(props) {
    super(props)

    this.scrollToBottom = this.scrollToBottom.bind(this)
  }

  scrollToBottom() {
    this.refs.myView.scrollToEnd()
  }

  render() {
    return {
      <ListView
           onLayout={this.scrollToBottom}
           ref='myView'
           ...
      />
   }
 }

Note: ListView is now deprecated.

Comments

0

This this worked for me.

<Content ref={c => (this.contentComponent = c)}>
....Any code
</Content >

This allows you to to use the scrollToPosition(0) function like

this.contentComponent._root.scrollToPosition(0);

Comments

0

I don't think it's good that the top answer is suggesting using a setTimeout, which sort of goes against React's design.

Imo, the best way to handle this (and what worked for me):

  1. Create and set a ref for the scrollable container
  2. Have the onLayout() method on the List element itself trigger the .scrollToBottom() method on the scrollable container ref.
const scrollContainer = useRef();

const scrollToEnd = () => {
  chatContainer.current.scrollToEnd();
}

<ScrollView ref={scrollContainer}>
  <FlatList onLayout={scrollToEnd}>
  </Flatlist>
<ScrollView>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.