1

I have the following Snack setup (please use Android version):

https://snack.expo.io/@sj458147/stackoverflow-unable-to-call-method

  1. When clicking Go to step 2 - I get undefined is not an object, the View should scroll by calling the method - moveToPage

  2. If a user was to swipe the Modal View, it would move to next the next View. How can I stop this interaction (the view should only scroll when the user clicks a button and not by swiping).

  3. Expo gives the message this.refs is deprecated, how would I update this code?

1 Answer 1

2

you need to use React.createRef() in your constructor. And use this reference in your component. In go() function, to achieve moveToPage(2) you need to use current of your reference like in the below;

class ShowModal2 extends Component {
  constructor(props){
    super(props);
    this.MyScrollView = React.createRef();
  }

  go = () => {
    this.MyScrollView.current.moveToPage(2);
  };

  render() {
    return (
      <Modal
        style={styles.modal}
        isVisible={true}
        onBackdropPress={this.hideModal}>
        <MyScrollView ref={this.MyScrollView}>
        ......

and apply same approach to other class

class MyScrollView extends Component {
  constructor() {
    super();
    this.state = { page: '' }
    this.scrollView = React.createRef();
  }
  moveToPage(page) {
    this.scrollView.current.scrollTo({ x: ((page - 1) * device_width), y: 0, 
    animated: true });
    alert(page);
  }

  render() {
    return (
      <View style={styles.container}>
        <ScrollView ref={this.scrollView} showsHorizontalScrollIndicator={false} horizontal={true} pagingEnabled={true}>
      ......

and check from link please-> https://snack.expo.io/@sdkcy/stackoverflow-unable-to-call-method

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you, perfect. Just one thing, currently the user can swipe to the next view, is there a way to stop this, the slide should only move when a button is pressed. Thanks
if you add scrollEnabled={false} to ScrollView component in MyScrollView class, it wont slide. Please try and let me know.

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.