Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/reactjs/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Reactjs React Native ScrollView scrollTo函数不工作_Reactjs_React Native - Fatal编程技术网

Reactjs React Native ScrollView scrollTo函数不工作

Reactjs React Native ScrollView scrollTo函数不工作,reactjs,react-native,Reactjs,React Native,我试图使滚动到顶部,如果满足某个条件,在组件的组件将接收道具事件中。。。但什么也没发生: componentWillReceiveProps(nextProps) { // some code... if (newQuery === query && this.scrollViewRef.current) { console.log('Should scroll to top'); // << logs successfully

我试图使滚动到顶部,如果满足某个条件,在组件的
组件将接收道具
事件中。。。但什么也没发生:

componentWillReceiveProps(nextProps) {
     // some code...

    if (newQuery === query && this.scrollViewRef.current) {
      console.log('Should scroll to top'); // << logs successfully

      this.scrollViewRef.current.scrollTo({
        x: 0,
        y: 0,
        duration: 500,
        animated: true,
      });
    }
  }
渲染方法:

render () {
   return (
      <ScrollView
        ref={this.scrollViewRef}
        contentContainerStyle={{ marginVertical: 8 }}
      >
    ...
    )
}
render(){
返回(
...
)
}
我还试图通过按下按钮手动滚动。。。也不管用


有什么帮助吗?

注意:奇怪的函数签名是因为,出于历史原因,函数也接受单独的参数作为选项对象的替代。由于不明确(x之前为y),不推荐使用此选项,因此不应使用此选项


如果您还使用带有ScrollView的FlatList,可以尝试使用scrollToOffset方法。

我发现了这一点

scrollView
在一个孤立的环境(一个全新的项目)中工作得非常好


我想问题可能在滚动视图的容器中。。。我发现父组件也有一个
滚动视图
。。。一旦我删除了它,一切都很好。

对于那些使用
useRef()
方法并获取
'xRef.scrollTo'不是函数的人来说,
错误,试着像
xRef.current.scrollTo({[您的参数]})那样使用它。


我不知道这个
当前的
事情,我快发疯了。

有错误消息吗?什么都没有。。。我记录了
this.scrollViewRef.current
,其中有scrollTo函数。您是否尝试过
this.scrollViewRef.getNode().scrollTo()
?不幸的是,它给出了这个错误:
this.scrollViewRef.getNode不是一个函数
…啊,很有趣,很高兴你解决了这个问题。谢谢你,但这是一个警告,不要使用scrollTo(x,y,动画)的旧签名。。。我们应该使用scollTo({x:5,y:5,animated:true})。。。这就是我目前正在使用的。。。。我问题中的代码没有抛出任何错误,甚至没有警告。你是否检查过这是否相似?是的,我在发布之前做了自己的研究。。。我遇到了一个stackoverflow post。。。试过里面的所有东西。。。也不管用。。。不过还是要谢谢你
render () {
   return (
      <ScrollView
        ref={this.scrollViewRef}
        contentContainerStyle={{ marginVertical: 8 }}
      >
    ...
    )
}