でカスタムを作成する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>
);
});