React Native ScrollView-如何从另一个子组件按钮滚动到子组件



我有一个带有scrollview的结构,这是一个有5个孩子的父母

带有scrollview的父组件

  • component1
  • component2
  • component3
  • component4
  • component5

在component3中,我有一个按钮,按下时应滚动parent parent component scrollview到component5

类似这样的东西

home(parent)

export default class Home extends React.Component {      
    renderComments() {
        return this.state.dataSource.map(item =>
            <CommentDetail key={item.id} comment={item} />
        );
    }
    render() {
        return (
            <ScrollView>      
                <Component1 />
                <Component2 />
                <CentralElements  {...this.state.dataSource} scroll = {this.props.scroll} />
                <Component4 />                  
                <View>
                    {this.renderComments()}
                </View>
            </ScrollView>
        );
    }
}

中心元素(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>
        );
    }
};

和评论是组件5,对父母滚动的任何想法吗?我试图弄清楚我缺少什么,因为那是我第一次接触。

我所做的是..
在component5中,我在主视图中调用onlayout,然后在父组件中保存 xy。要在Component 3中滚动到它,请单击"单击我"调用scrollview ref滚动到存储在

之前存储的值的父函数

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>
        )
    }
}

父母:

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>
        )
    }
}

相关内容

  • 没有找到相关文章

最新更新