javascript - React Native ScrollView - How to scroll to a child component from another child component button? - Stack Overflow

I have this structure with a ScrollView, which is a parent with 5 childsParent Component with ScrollVie

I have this structure with a ScrollView, which is a parent with 5 childs

Parent Component with ScrollView

  • Component1
  • Component2
  • Component3
  • Component4
  • Component5

Inside Component3 I have a button that when pressed should scroll parent ponent ScrollView to Component5

Something like this

Home (Parent)

export default class Home extends React.Component {      
    renderComments() {
        return this.state.dataSource.map(item =>
            <CommentDetail key={item.id} ment={item} />
        );
    }

    render() {
        return (
            <ScrollView>      
                <Component1 />
                <Component2 />
                <CentralElements  {...this.state.dataSource} scroll = {this.props.scroll} />
                <Component4 />                  
                <View>
                    {this.renderComments()}
                </View>
            </ScrollView>
        );
    }
}

I have this structure with a ScrollView, which is a parent with 5 childs

Parent Component with ScrollView

  • Component1
  • Component2
  • Component3
  • Component4
  • Component5

Inside Component3 I have a button that when pressed should scroll parent ponent ScrollView to Component5

Something like this

Home (Parent)

export default class Home extends React.Component {      
    renderComments() {
        return this.state.dataSource.map(item =>
            <CommentDetail key={item.id} ment={item} />
        );
    }

    render() {
        return (
            <ScrollView>      
                <Component1 />
                <Component2 />
                <CentralElements  {...this.state.dataSource} scroll = {this.props.scroll} />
                <Component4 />                  
                <View>
                    {this.renderComments()}
                </View>
            </ScrollView>
        );
    }
}

CentralElements (Component3)

export default class CentralElements extends React.Component {
    constructor(props) {
        super(props);
    }
  
    goToComments= () => {
        this.props.scroll.scrollTo({x: ?, y: ?, animated: true});
     };

    render() {
        return (
            <ScrollView horizontal={true}>
                <TouchableOpacity onPress={this.goToComments}>
                    <Image source={require('../../assets/image.png')} />
                    <Text>Comments</Text>
                </TouchableOpacity>
                ...
                </TouchableOpacity>
            </ScrollView>
        );
    }
};

And the Comments are the Component5, any idea on how to the parent scroll? I trying to figure what I'm missing, since thats my first contact with this.

Share Improve this question edited Feb 28, 2019 at 15:29 Arthur Ávila asked Feb 28, 2019 at 15:15 Arthur ÁvilaArthur Ávila 252 silver badges6 bronze badges 1
  • Can you please have a look at the answer here stackoverflow./a/56065152/8031495 You can make some changes and can achieve the desired solution. – snehal agrawal Commented May 9, 2019 at 18:09
Add a ment  | 

1 Answer 1

Reset to default 3

What i did was..
in ponent5 I call onLayout in the main view and then save x and y in the parent ponent. To scroll to it in ponent 3 on click i call the parent function that uses the scrollview ref to scroll to the values stored before

Component5

    export default class Component5 extends Component {

    saveLayout() {
        this.view.measureInWindow((x, y, width, height) => {
            this.props.callParentFunction(x, y)
        })
    }
    render() {
        return (
            <View ref={ref => this.view = ref} onLayout={() => this.saveLayout()}>

            </View>
        )
    }
}

Component3

export default class Component3 extends Component {

    render() {
        return (
            <View >
                <TouchableOpacity onPress={()=>{this.props.goToComponent5()}}>

                </TouchableOpacity>
            </View>
        )
    }
}

Parent:

export default class Parent extends Component {
constructor(props) {
this.goToComponent5=this.goToComponent5.bind(this)
    super(props)
    this.state = {
        x:0,
        y:0,
    }
}

    callParentFunction(x, y) {
        this.setState({ x, y })
    }

    goToComponent5(){
        this.ScrollView.scrollTo({x: this.state.x, y: this.state.y, animated: true});
    }

    render() {
        return (
            <View >
                <ScrollView ref={ref => this.ScrollView = ref}>
                    <Component1 />
                    <Component2 />
                    <Component3 goToComponent5={this.goToComponent5}/>
                    <Component4 />
                    <Component5 callParentFunction={this.callParentFunction}/>
                </ScrollView>
            </View>
        )
    }
}

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745385813a4625433.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信