我有一个包含子React组件的父React组件.
<div>
<div>Child</div>
</div>
Run Code Online (Sandbox Code Playgroud)
我需要将样式应用于子组件以将其定位在其父组件中,但其位置取决于父组件的大小.
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>;
}
Run Code Online (Sandbox Code Playgroud)
我不能在这里使用百分比值,因为顶部和左侧位置是孩子和父母的宽度和高度的函数.
React的方法是什么?