【发布时间】:2020-02-23 17:02:56
【问题描述】:
对 React 还是很陌生,我遇到了以下问题: 我正在以这种方式创建一个组件:
export const CSpiderWeb = (props: iSpiderWebProps) => {
const classes = useStyles();
const [drawingObject, setDrawingObject] = useState({} as iDrawingObject);
const _InitRaphael = (target : HTMLDivElement) => {
while (target.firstChild) {
target.removeChild(target.firstChild);
}
const workDrawingObject : iDrawingObject = {
width : target.offsetWidth,
height : target.offsetHeight,
centerX : target.offsetWidth / 2,
centerY : target.offsetHeight /2
}
workDrawingObject.paper = Raphael(target, workDrawingObject.width, workDrawingObject.height);
setDrawingObject(workDrawingObject);
}
var workRef = createRef<HTMLDivElement>();
_InitRaphael(workRef.current as HTMLDivElement);
return <div ref={workRef} className={classes.paperContainer}>{drawingObject.centerX}x{drawingObject.centerY}</div>
}
我在这里尝试完成的是获取渲染的 DIV 元素并将其传递给 _InitRaphael 方法,但似乎这是在渲染元素之前调用的。 有道理,但如何做到这一点。我用谷歌搜索和搜索,有时我遇到了 componentDidMount 钩子,但可以在这里使用吗?如果是这样,那么如何?
【问题讨论】:
标签: reactjs typescript components