我有一个包含一个子 React 组件的父 React 组件。
<div>
<div>Child</div>
</div>
我需要为子组件应用样式,以在其父组件内定位,但它的位置取决于父组件的大小。render() {
const styles = {
position: 'absolute',
top: top(), // computed based on child and parent's height
left: left() // computed based on child and parent's width
};
return <div style={styles}>Child</div>;
}
因为子元素和父元素的宽度和高度是功能函数,所以我无法在此处使用百分比值。
React 的实现方式是什么?