【发布时间】:2019-03-23 17:28:19
【问题描述】:
我正在尝试滚动到顶部,如果满足某个条件,在组件的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,
});
}
}
我如何为 scrollView 创建 ref 的代码 sn-p:
class SearchResult extends React.Component {
constructor(props) {
super(props);
this.scrollViewRef = React.createRef();
}
//...
}
渲染方法:
render () {
return (
<ScrollView
ref={this.scrollViewRef}
contentContainerStyle={{ marginVertical: 8 }}
>
...
)
}
我还尝试通过按下按钮手动滚动...也不起作用
有什么帮助吗?
【问题讨论】:
-
有错误提示吗?
-
什么都没有……我登录了
this.scrollViewRef.current,并且有scrollTo函数 -
你试过
this.scrollViewRef.getNode().scrollTo()吗? -
不幸的是,它给出了这个错误:
this.scrollVieeRef.getNode is not a function... -
啊有趣,很高兴你解决了它。
标签: reactjs react-native