$ git show layout-toggle
List / grid toggle
Switch a list of cards into a two-column grid and every card glides and resizes into its new spot.
// Unchanged in Reanimated 4: layout transitions animate size and position
import Animated, { LinearTransition } from 'react-native-reanimated';
const [grid, setGrid] = useState(false);
const { width } = useWindowDimensions();
const GAP = 10, PAD = 16;
const cell = (width - PAD * 2 - GAP) / 2;
const layout = LinearTransition.springify().damping(18).stiffness(160);
<Pressable onPress={() => setGrid((g) => !g)}>
<Text>{grid ? 'List view' : 'Grid view'}</Text>
</Pressable>
{/* A plain View, not a FlatList: layout animations need every item to stay mounted */}
<View style={{ flexDirection: 'row', flexWrap: 'wrap', gap: GAP, padding: PAD }}>
{items.map((item) => (
<Animated.View
key={item.id}
layout={layout}
style={[styles.card, grid
? { width: cell, height: cell }
: { width: '100%', height: 72, flexDirection: 'row' }]}>
<Animated.Image layout={layout} source={item.image}
style={grid ? { width: '100%', height: cell * 0.65 } : { width: 72, height: 72 }} />
<Text>{item.title}</Text>
</Animated.View>
))}
</View>