Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

I want to use scrollTo

I work on a project in React Native and I would like to set my ScrollView position. So I search and I found we should do this with scrollTo but I have an error:

TypeError: Cannot read property 'scrollTo' of undefined

My code:

export default class Index_calendar extends Component {
 componentDidMount() {
   const _scrollView = this.scrollView;
   _scrollView.scrollTo({x: 100});
 }

 render() {
   return (
     <ScrollView ref={scrollView => this.scrollView = scrollView}>
       {this.renderCalandar()}
     </ScrollView>
   );
 }
}
like image 533
Eliott Robert Avatar asked Dec 24 '22 12:12

Eliott Robert


1 Answers

You can use the InteractionManager to solve this issue. For instance

InteractionManager.runAfterInteractions(() => this.scroll.current.scrollTo({ x }));

like image 143
wcandillon Avatar answered Feb 27 '23 02:02

wcandillon