Qiita Teams that are logged in
You are not logged in to any team

Log in to Qiita Team
Community
OrganizationAdvent CalendarQiitadon (β)
Service
Qiita JobsQiita ZineQiita Blog
Help us understand the problem. What is going on with this article?

react-navigation + react-native-parallax-scroll-view でタブアイコンが押されたらParallaxScrollViewをScrollToTop

More than 1 year has passed since last update.

react-navigator + react-native-parallax-scroll-view でタブアイコンが押されたらParallaxScrollViewをScrollToTop

ライブラリのソースに下記を追加

/node_modules/react-native-parallax-scroll-view/src/index.js
    componentDidMount() {
      this.props.navigation.setParams({
        scrollToTop: ()=>{
                this._scrollView._component.scrollTo({ x: 0, y: 0, animated: true })
        },
      });
    }

※上記のソースはrefの部分を変更してます。元のままだったらこっち

/node_modules/react-native-parallax-scroll-view/src/index.js
    componentDidMount() {
      this.props.navigation.setParams({
        scrollToTop: ()=>{
                this.refs[SCROLLVIEW_REF]._component.scrollTo({ x: 0, y: 0, animated: true })
        },
      });
    }

その後、下記のようにnavigationを渡したあげる

component.js
<ParallaxScrollView
      navigation = { this.props.navigation }/*←ここ*/
        ref={(ref)=>{ this._ParallaxScrollView=ref; }}
        backgroundColor="rgba(255,255,255,0.8)"
        contentBackgroundColor="#ddd"
        stickyHeaderHeight={40}
        renderStickyHeader={() => (
          /*code*/
        )}
        parallaxHeaderHeight={270}
        renderForeground={() => (
           /*code*/
        )}>
          /*code*/
      </ParallaxScrollView>

react-navigationの設定は下記

navigation.js
const tabNav = TabNavigator({

    User:{
        screen: User,
        navigationOptions:({ navigation })=>({
            tabBarIcon: ({tintColor})=><Icon color={tintColor} name={"account"} size={35} />,
             tabBarOnPress:(({previousScene, scene, jumpToIndex})=>{
                const { route, index, focused} = scene;
                if(focused){
                    navigation.state.params.scrollToTop();
                }
                jumpToIndex(0)
            }),
        }),
    },

});
shoichi1023
とあるIT企業に所属するエンジニアです。 投稿する記事の内容は、個人の見解であり、所属する組織の公式見解ではありません。
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away