【发布时间】:2016-09-12 22:10:08
【问题描述】:
我需要能够通过它的 offsetHeight 来定位 React DOM 元素。
问题是我无法收集尚未创建的元素的 offsetHeight(因此无法将高度作为参数传递给渲染函数),我也无法计算渲染内部的高度功能如 React DOM refs 文档中所述:
永远不要在任何组件的 render 方法中访问 refs - 或者当任何组件的 render 方法甚至在调用堆栈中的任何位置运行时。
DOM 元素应相对于单击以显示它的图标呈现。
组件树:
|— FormInputGroup
|— Label
|— TooltipIcon
|— Tooltip
|— Input
工具提示图标的渲染函数:
const TooltipIcon = ({ attribute, setCurrentTooltip }) => (
<Icon
name="tooltip"
style={{
position: "relative",
top: "1px",
marginLeft: "10px",
"cursor": "pointer",
}}
onClick={() => setCurrentTooltip(attribute)}
/>
)
渲染函数:
const Tooltip = ({ title, content, setCurrentTooltip }) => (
<div className="popover"
style={{
// top: "-"+ ReactDOM.findDOMNode(this).offsetHeight +"px",
}}
>
<h3 className="popover-title">{title}</h3>
<div className="popover-close-button"
onClick={() => setCurrentTooltip(null)}
/>
<div className="popover-content">{content}</div>
</div>
)
这是没有定位的 DOM 元素: not positioned
这是我想要的位置渲染方式(顶部:-(offsetHeight)px: positioned
【问题讨论】:
标签: javascript css reactjs