我使用新的FlatList
component並希望在生命週期方法(如componentDidMount
)中使用ScrollToIndex
(或ScrollToEnd
)。FlatList組件生命週期方法ScrollToIndex ScrollToEnd等
我有一個讓我們說100個項目的數組,我不想開始渲染第一個項目,但在開始。讓我們說在第50項。當FlatList
僅一次顯示一個項目,它的工作根據需要與一些黑客如下所述:https://github.com/facebook/react-native/issues/13202
componentDidMount() {
let wait = new Promise((resolve) => setTimeout(resolve, 500)); // Smaller number should work
wait.then(() => {
this.refs.flatlist.scrollToIndex({index: 4, animated: true});
});
}
這個片段使得scrollToIndex
運行後一些componentDidMount
調用的毫秒。
但是,當我使用FlatList
其中視圖包含3x3網格,我根本無法使它運行。當我使用scrollToIndex
並且索引超出指定的道具initialNumToRender
時,我只收到一個我無法理解的錯誤scrollIndex out of range $ID
。提供的數據數組包含所有的數據,例如100個項目。當我使我們成爲scrollToEnd
時,它只停留在中間的某個地方而不是最後。對我來說,它看起來像某種錯誤,我不知道如何在初始渲染後滾動到$ X項目。你可以幫我嗎?
我很感激任何形式的幫助或評論。
我在iOS和Android(仿真器和設備)上嘗試了React-Native v0.43.0和v0.44.0,它總是一樣的。