我正在尝试在组件中增加子项的高度,以实现动画。
我想获取子组件的高度,以便可以对高度值进行动画处理,以实现动画扩展。
{expanded ? (
<>
<Animated.View style={{height: someAnimatedHeight}}>
<View onLayout={handleLayout}>
{children}
</View>
</Animated.View>
<PromoCodeArea />
</>
) : (
<PromoCodeArea />
)}
handleLayout
const handleLayout = ({ nativeEvent }) => {
setContainerHeight(nativeEvent.layout.height);
};
但是,handleLayout
不会提供高度,直到扩展卡为止。我可以使动画以硬编码的大小工作。
我希望动画灵活并扩展到孩子们的身高。
未展开时,handleLayout
返回值为0,因此无法设置toValue
:
const startAnimation = () => {
Animated.spring(heightAnimation,{
toValue: 125,<-- like this to be based on the height of children
}).start();
};
我无法找到使用功能组件和挂钩实现此目的的示例。