React Native ScrollView的scrollTo函数不起作用

Hen*_*hli 6 reactjs react-native

如果在组件的事件中满足特定条件,我试图滚动到顶部componentWillReceiveProps......但什么也没有发生:

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,
      });
    }
  }
Run Code Online (Sandbox Code Playgroud)

我如何为滚动视图创建引用的代码片段:

class SearchResult extends React.Component {
  constructor(props) {
    super(props);

    this.scrollViewRef = React.createRef();
  }
//...
}
Run Code Online (Sandbox Code Playgroud)

渲染方法:

render () {
   return (
      <ScrollView
        ref={this.scrollViewRef}
        contentContainerStyle={{ marginVertical: 8 }}
      >
    ...
    )
}
Run Code Online (Sandbox Code Playgroud)

我还尝试通过按下按钮手动滚动......也不起作用

有什么帮助吗?

Hen*_*hli 3

我明白了这一点...

scrollView在一个孤立的环境(一个全新的项目)中完美地工作......

我认为问题可能出在该滚动视图的容器中......我发现父组件也有一个ScrollView......一旦我删除它,一切都工作正常。