Skip to content

Function: useSharedHero() ​

ts
function useSharedHero(initial?): object;

State helper for in-place (non-navigation) shared-hero transitions within the same screen.

Render two <SharedHero id="x"> branches keyed on active (for example, a collapsed pill and an expanded card). Calling start(), end(), or toggle() swaps which branch is mounted, and the native coordinator matches the unmounting and mounting views within the same frame to run a flight.

Navigation-driven transitions (React Navigation, Expo Router, <Modal>) do not need this hook.

Parameters ​

ParameterTypeDefault valueDescription
initial?booleanfalseInitial value of active. Defaults to false.

Returns ​

An object with the current active boolean and stable start, end, and toggle callbacks.

NameTypeDescription
activebooleanWhether the transition is currently in the active (expanded) state.
end()() => voidSets active to false.
start()() => voidSets active to true.
toggle()() => voidFlips active between false and true.

Example ​

tsx
const { active, toggle } = useSharedHero();

return (
  <Pressable onPress={toggle}>
    {active ? (
      <SharedHero id="player" mode="morph" style={styles.expanded}>
        <ExpandedPlayer />
      </SharedHero>
    ) : (
      <SharedHero id="player" mode="morph" style={styles.mini}>
        <MiniPlayer />
      </SharedHero>
    )}
  </Pressable>
);

Released under the MIT License.