zachgibson / react-native-parallax-swiper

Paged Parallax Swiper with Effects
MIT License
623 stars 76 forks source link

Typo - scrollToIndex is not Function but a Number #10

Closed bobmulder closed 6 years ago

zachgibson commented 6 years ago

@bobmulder Updated your change to include that it needs to be state and fixed a typo. Does this description make sense to you now?

bobmulder commented 6 years ago

Thanks for the addition. This makes more sense now!

zachgibson commented 6 years ago

@bobmulder Hey I realized a better way to handle initial scrollToIndex. ParallaxSwiper.js needs to call scrollToIndex() in componentDidMount() if it exists.

Like:

componentDidMount() {
  const { scrollToIndex } = this.props;

  if (scrollToIndex) {
    this.scrollToIndex(scrollToIndex);
  }
}

This way you can just pass state and it will scroll to the index specified in initial state on mount. Can you implement this? Then I can merge the PR. 👍

bobmulder commented 6 years ago

Am I right this is done now via componentWillReceiveProps at ParallaxSwiper.js and you want to move it to componentDidMount?

zachgibson commented 6 years ago

We want to have it in both places. So it scrolls to the index specified in user’s initial state on component mount and then works when they update it.

In this snippet, ParallaxSwiper will scroll to index 3 when it first mounts via componentDidMount. Then anytime this.state.index is changed it will update via componentWillReceiveProps.

state = { index: 3 }

...

<ParallaxSwiper scrollToIndex={this.state.index}>
...
</ParallaxSwiper>
bobmulder commented 6 years ago

Ahh got it! I'll create a PR to discuss this code. Your code suggestion looks fine to me?