logo by @sawaratsuki1004
React
v19.2
Learn
Reference
Community
Blog

Is this page useful?

في هذه الصفحة

  • Overview
  • Reference
  • addTransitionType
  • Usage
  • Adding the cause of a transition
  • Customize animations using browser view transition types
  • Customize animations using View Transition Class
  • Customize animations using ViewTransition events

    react@19.2

  • نظرة عامة
  • Hooks
    • useActionState
    • useCallback
    • useContext
    • useDebugValue
    • useDeferredValue
    • useEffect
    • useEffectEvent
    • useId
    • useImperativeHandle
    • useInsertionEffect
    • useLayoutEffect
    • useMemo
    • useOptimistic
    • useReducer
    • useRef
    • useState
    • useSyncExternalStore
    • useTransition
  • المكونات
    • <Fragment> (<>)
    • <Profiler>
    • <StrictMode>
    • <Suspense>
    • <Activity>
    • <ViewTransition> - This feature is available in the latest Canary version of React
  • APIs
    • act
    • addTransitionType - This feature is available in the latest Canary version of React
    • cache
    • cacheSignal
    • captureOwnerStack
    • createContext
    • lazy
    • memo
    • startTransition
    • use
    • experimental_taintObjectReference - This feature is available in the latest Experimental version of React
    • experimental_taintUniqueValue - This feature is available in the latest Experimental version of React
  • react-dom@19.2

  • Hooks
    • useFormStatus
  • المكونات (Components)
    • Common (e.g. <div>)
    • <form>
    • <input>
    • <option>
    • <progress>
    • <select>
    • <textarea>
    • <link>
    • <meta>
    • <script>
    • <style>
    • <title>
  • APIs
    • createPortal
    • flushSync
    • preconnect
    • prefetchDNS
    • preinit
    • preinitModule
    • preload
    • preloadModule
  • Client APIs
    • createRoot
    • hydrateRoot
  • Server APIs
    • renderToPipeableStream
    • renderToReadableStream
    • renderToStaticMarkup
    • renderToString
    • resume
    • resumeToPipeableStream
  • Static APIs
    • prerender
    • prerenderToNodeStream
    • resumeAndPrerender
    • resumeAndPrerenderToNodeStream
  • React Compiler

  • الإعدادات (Configuration)
    • compilationMode
    • gating
    • logger
    • panicThreshold
    • target
  • Directives
    • "use memo"
    • "use no memo"
  • تصريف المكتبات (Compiling Libraries)
  • React DevTools

  • React Performance tracks
  • eslint-plugin-react-hooks

  • Lints
    • exhaustive-deps
    • rules-of-hooks
    • component-hook-factories
    • config
    • error-boundaries
    • gating
    • globals
    • immutability
    • incompatible-library
    • preserve-manual-memoization
    • purity
    • refs
    • set-state-in-effect
    • set-state-in-render
    • static-components
    • unsupported-syntax
    • use-memo
  • قواعد React (Rules of React)

  • نظرة عامة (Overview)
    • Components و Hooks يجب أن تكون Pure
    • React تستدعي Components و Hooks
    • قواعد Hooks
  • React Server Components

  • Server Components
  • Server Functions
  • Directives
    • 'use client'
    • 'use server'
  • Legacy APIs

  • Legacy React APIs
    • Children
    • cloneElement
    • Component
    • createElement
    • createRef
    • forwardRef
    • isValidElement
    • PureComponent
مرجع API
APIs

addTransitionType - This feature is available in the latest Canary version of React

Canary

The addTransitionType API is currently only available in React’s Canary and Experimental channels.

Learn more about React’s release channels here.

addTransitionType lets you specify the cause of a transition.

startTransition(() => { addTransitionType('my-transition-type'); setState(newState); });

  • Reference
    • addTransitionType
  • Usage
    • Adding the cause of a transition
    • Customize animations using browser view transition types
    • Customize animations using View Transition Class
    • Customize animations using ViewTransition events

Reference

addTransitionType

Parameters

  • type: The type of transition to add. This can be any string.

Returns

startTransition does not return anything.

Caveats

  • If multiple transitions are combined, all Transition Types are collected. You can also add more than one type to a Transition.
  • Transition Types are reset after each commit. This means a <Suspense> fallback will associate the types after a startTransition, but revealing the content does not.

Usage

Adding the cause of a transition

Call addTransitionType inside of startTransition to indicate the cause of a transition:

import { startTransition, addTransitionType } from 'react'; function Submit({action) { function handleClick() { startTransition(() => { addTransitionType('submit-click'); action(); }); } return <button onClick={handleClick}>Click me</button>; }

When you call addTransitionType inside the scope of startTransition, React will associate submit-click as one of the causes for the Transition.

Currently, Transition Types can be used to customize different animations based on what caused the Transition. You have three different ways to choose from for how to use them:

  • Customize animations using browser view transition types
  • Customize animations using View Transition Class
  • Customize animations using ViewTransition events

In the future, we plan to support more use cases for using the cause of a transition.


Customize animations using browser view transition types

When a ViewTransition activates from a transition, React adds all the Transition Types as browser view transition types to the element.

This allows you to customize different animations based on CSS scopes:

function Component() { return ( <ViewTransition> <div>Hello</div> </ViewTransition> ); } startTransition(() => { addTransitionType('my-transition-type'); setShow(true); });

:root:active-view-transition-type(my-transition-type) { &::view-transition-...(...) { ... } }


Customize animations using View Transition Class

You can customize animations for an activated ViewTransition based on type by passing an object to the View Transition Class:

function Component() { return ( <ViewTransition enter={{ 'my-transition-type': 'my-transition-class', }}> <div>Hello</div> </ViewTransition> ); } // ... startTransition(() => { addTransitionType('my-transition-type'); setState(newState); });

If multiple types match, then they’re joined together. If no types match then the special “default” entry is used instead. If any type has the value “none” then that wins and the ViewTransition is disabled (not assigned a name).

These can be combined with enter/exit/update/layout/share props to match based on kind of trigger and Transition Type.

<ViewTransition enter={{ 'navigation-back': 'enter-right', 'navigation-forward': 'enter-left', }} exit={{ 'navigation-back': 'exit-right', 'navigation-forward': 'exit-left', }}>


Customize animations using ViewTransition events

You can imperatively customize animations for an activated ViewTransition based on type using View Transition events:

<ViewTransition onUpdate={(inst, types) => { if (types.includes('navigation-back')) { ... } else if (types.includes('navigation-forward')) { ... } else { ... } }}>

This allows you to pick different imperative Animations based on the cause.

السابقact
التاليcache

Copyright © Meta Platforms, Inc
no uwu plz
uwu?
Logo by@sawaratsuki1004
تعلم React
بداية سريعة
التثبيت
وصف واجهة المستخدم (UI)
إضافة التفاعلية
إدارة State
مخارج الطوارئ
مرجع API
React APIs
React DOM APIs
المجتمع
ميثاق السلوك
تعرف على الفريق
المساهمون في التوثيق
شكر وتقدير
المزيد
المدونة
React Native
الخصوصية
الشروط
startTransition(() => {
addTransitionType('my-transition-type');
setState(newState);
});
import { startTransition, addTransitionType } from 'react';

function Submit({action) {
function handleClick() {
startTransition(() => {
addTransitionType('submit-click');
action();
});
}

return <button onClick={handleClick}>Click me</button>;
}
function Component() {
return (
<ViewTransition>
<div>Hello</div>
</ViewTransition>
);
}

startTransition(() => {
addTransitionType('my-transition-type');
setShow(true);
});
:root:active-view-transition-type(my-transition-type) {
&::view-transition-...(...) {
...
}
}
function Component() {
return (
<ViewTransition enter={{
'my-transition-type': 'my-transition-class',
}}>
<div>Hello</div>
</ViewTransition>
);
}

// ...
startTransition(() => {
addTransitionType('my-transition-type');
setState(newState);
});
<ViewTransition enter={{
'navigation-back': 'enter-right',
'navigation-forward': 'enter-left',
}}
exit={{
'navigation-back': 'exit-right',
'navigation-forward': 'exit-left',
}}>
<ViewTransition onUpdate={(inst, types) => {
if (types.includes('navigation-back')) {
...
} else if (types.includes('navigation-forward')) {
...
} else {
...
}
}}>