Cơ BảnReact Native iconReact Native

`LayoutAnimation` — khi nào đủ dùng?

LayoutAnimation là built-in API — khai báo "tôi muốn next layout change được animate", và RN tự interpolate giữa layout cũ và mới.

tsx
import { LayoutAnimation, UIManager, Platform } from 'react-native'

if (Platform.OS === 'android') {
  UIManager.setLayoutAnimationEnabledExperimental?.(true)
}

const toggle = () => {
  LayoutAnimation.configureNext(LayoutAnimation.Presets.easeInEaseOut)
  setExpanded(!expanded)
}

Use case phù hợp:
- Add/remove item trong list ngắn (FlatList animation chuyển vị trí).
- Expand/collapse accordion.
- Show/hide UI block khi state đổi.

Hạn chế:
- Animation đơn giản — chỉ có 3 preset (easeInEaseOut, linear, spring).
- Khó custom: timing fix, interpolation curve hạn chế.
- Apply cho toàn bộ tree trong tick đó — không scope được vào component cụ thể.
- Conflict với Reanimated layout animation nếu dùng song song.

Khi nào dùng cái khác:
- Cần animation precise → Reanimated entering={FadeIn}, layout={Layout.springify()}.
- Cần gesture-driven → Reanimated worklet.
- Cần per-item animation trong list dài → FlashList với cellAnimation.

Thực tế 2026: dùng LayoutAnimation cho prototype nhanh hoặc UI rất đơn giản. Production lớn dùng Reanimated. RN dev khuyến nghị deprecate LayoutAnimation sau khi Reanimated layout API stable.

Xem toàn bộ React Native cùng filter theo level & chủ đề con.

Mở danh sách React Native