2
我目前有一個Feed
組件。它接受一個data
prop,這是一個項目數組,並且支持一個children
prop,該prop用於將數據映射到DOM元素。保存ref作爲子功能創建的子項
現在,我正在嘗試創建功能以滾動到由children
函數映射的此data
屬性中的任何元素。但是,這意味着我需要存儲對它的引用。我不確定適當的語法/ api。這裏是我的基本代碼:
class ScrollableFeed extends Component {
static propTypes = {
data: PropTypes.array,
children: PropTypes.func,
};
container = null;
render() {
const { data, children } = this.props;
return (
<div
className={styles.feed}
onScroll={this.onScroll}
ref={e => { this.container = e; }}
>
{data.map(children)}
</div>
);
}
}
爲了滾動到一個組成部分,我將需要已經由children
函數映射出的DOM元素的引用。
這怎麼能做到反應?
你可以分享'Feed'組件的代碼嗎? –
哎呀抱歉,它是'ScrollableFeed'的代碼,只是類型不匹配 – corvid
好吧,我的意思是代表'ScrollableFeed'的父代碼。 –