簡體   English   中英

React Native:如何專注於ListView?

[英]React Native: How can I focus on ListView?

我一直在致力於ListView組件。
這是我的代碼

class MyListView extends Component { 
    static propTypes = {
        navigator: PropTypes.object.isRequired,
    };

    componentDidMount() {
        this.listView.focus()
    }

    render() {
        return (
            <ListView
                ref={ref => { this.listView = ref }}
                contentContainerStyle={styles.listView}
                dataSource={cloneWithRows(listDataSource)}
                renderRow={(data) => <MyListViewCell
                    ...
                />}
            />
        )
    }
}
export default MyListView

但是它只返回諸如“ this.listView.focus不是函數”之類的錯誤。我已經搜索了很多文章,但是找不到解決方案。
有任何想法嗎?

componentDidMount不保證在調用componentDidMount之前先調用ref屬性。 您應該將componentDidMount代碼更改為以下內容:

componentDidMount() {
    requestAnimationFrame(()=>{
       this.listView.focus()
   })

}

另外,如果您決定使用requestAnimationFrame函數。 最好通過TimerMixin使用它(請參見TimerMixin doc )。 根據TimerMixin文檔中的建議,使用react-mixin在ES6中實現Mixin。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM