要创建自定义View
,translateY
我必须使用onLayout
. 这工作得很好,但今天我添加了一个Accordion
也可以动画的组件。这恰好触发onLayout
函数在每个计算帧上呈现,这使我的应用程序非常慢。我试过添加useCallback
和LayoutAnimation
来解决这个问题,但这似乎都不起作用。
有没有办法onLayout
在动画之前和之后触发唯一的?我考虑过为onLayout
.
export default memo(({ translateY }) => {
const [containerHeight, setContainerHeight] = useState(0);
const [contentHeight, setContentHeight] = useState(0);
console.log('render', containerHeight, contentHeight);
const onLayoutContainer = useCallback(event => {
LayoutAnimation.configureNext(LayoutAnimation.Presets.spring);
const height = event.nativeEvent.layout.height;
setContainerHeight(height);
}, []);
const onLayoutContent = useCallback(event => {
LayoutAnimation.configureNext(LayoutAnimation.Presets.spring);
const height = event.nativeEvent.layout.height;
setContentHeight(height);
}, []);
return (
<View onLayout={onLayoutContainer}>
<Animated.View
onLayout={onLayoutContent}
style={{ transform: [{ translateY }] }}
>
<Accordion />
<Accordion />
<Accordion />
<Accordion />
<Accordion />
<Accordion />
</Animated.View>
</View>
);
});