![](/img/trans.png)
[英]How can I get the ref or info of the React component that is scrolled into view during a vertical scroll using React Scrollbars?
[英]How can I get component's position by ref in React?
我正在為我的應用程序使用React 15.3.1。 所以,我需要在它的父級中獲得Component
x和y位置。 孩子像這樣呈現:
<Icon key={key} ref={(c) => this['icon' + key] = c}}/>;
這就是我嘗試訪問Icon
(基本上是div
)的位置:
let icon = this['icon' + this.state.currentIcon.id];
icon.getBoundingClientRect(); //Error: "getBoundingClientRect" is not a function
孩子是對的,我可以在調試器中看到它的props
。 但我看不到任何屬性,如getBoundingClientRect
, left
, top
或任何其他位置屬性。 得到它們我需要做什么?
你的ref
將引用Icon
,我猜它是一個React組件。 在getBoundingClientRect
方法可用之前,您需要解析實際的React Element(DOM元素)。
您可以通過ReactDOM.findDOMNode()函數執行此操作。
let icon = this['icon' + this.state.currentIcon.id];
const domNode = ReactDOM.findDOMNode(icon);
domNode.getBoundingClientRect()
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.