React Native ScrollView - How to scroll to a child component from another child component button?(React Native ScrollView - 如何从另一个子组件按钮滚动到子组件?)
问题描述
我有一个带有 ScrollView 的结构,它是一个有 5 个孩子的父级
I have this structure with a ScrollView, which is a parent with 5 childs
带有 ScrollView 的父组件
Parent Component with ScrollView
- 组件1
- 组件2
- 组件3
- 组件4
- 组件5
在 Component3 内部,我有一个按钮,按下该按钮应将父组件 ScrollView 滚动到 Component5
Inside Component3 I have a button that when pressed should scroll parent component ScrollView to Component5
类似的东西
首页(父)
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>
);
}
}
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>
);
}
};
评论是Component5,关于如何父滚动的任何想法?我试图弄清楚我错过了什么,因为那是我第一次接触这个.
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.
推荐答案
我做的是..
在 component5 中,我在主视图中调用 onLayout,然后将 x
和 y
保存在父组件中.要在单击时在组件 3 中滚动到它,我调用父函数,该函数使用 scrollview ref 滚动到之前存储的值
What i did was..
in component5 I call onLayout in the main view and then save x
and y
in the parent component.
To scroll to it in component 3 on click i call the parent function that uses the scrollview ref to scroll to the values stored before
组件5
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>
)
}
}
组件3
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>
)
}
}
这篇关于React Native ScrollView - 如何从另一个子组件按钮滚动到子组件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:React Native ScrollView - 如何从另一个子组件按钮滚动到子组件?
基础教程推荐
- 用于 Twitter 小部件宽度的 HTML/CSS 2022-01-01
- 我可以在浏览器中与Babel一起使用ES模块,而不捆绑我的代码吗? 2022-01-01
- html表格如何通过更改悬停边框来突出显示列? 2022-01-01
- 如何使用JIT在顺风css中使用布局变体? 2022-01-01
- Electron 将 Node.js 和 Chromium 上下文结合起来意味着 2022-01-01
- Chart.js 在线性图表上拖动点 2022-01-01
- Vue 3 – <过渡>渲染不能动画的非元素根节点 2022-01-01
- 自定义 XMLHttpRequest.prototype.open 2022-01-01
- 如何使用TypeScrip将固定承诺数组中的项设置为可选 2022-01-01
- 直接将值设置为滑块 2022-01-01