These days it's fairly easy to animate transitions between your app routes using
React components. But it adds difficulty to other things, like focusing an
input when a form appears. If the form is part of the initial page load, the
input should be focused immediately after mounting. But if you transition to
the form from some other route, the input shouldn't be focused until the
transition ends. So you need to know if a transition is in progress and take
action when it ends.
To make matters worse, there may be nested transitions. For example, in my app,
I have a fade transition between the /users and /organizations routes, but
within the /users route, I have a drilldown transition from /users to
/users/:userId. If the user is in /organizations/yolo and clicks a link to
/users/jimbo, there will be a fade transition, but not a drilldown transition.
So the /users/:userId form needs to know if either the fade or drilldown
transition is going before it focuses an input.
react-transition-context solves this problem. If the fade and the drilldown
both put use it to put their transition state on React context, the
/users/:userId form can register a effect to get called when it's fully
in (rather than mounted but appearing or entering).
TransitionState enum
There are five possible values:
'appearing'
'entering'
'in'
'leaving'
'out'
Overall transition state
The overall transition state takes into account both a transition component's
own transition state, and the overall transition state of its closest ancestor
transition component.
For example:
If the ancestor is 'in' and the descendant is 'entering', the overall transition state is 'entering'.
If the ancestor is 'leaving' and the descendant is 'entering', the overall transition state is 'leaving'.
If the ancestor is 'out' and the descendant is 'leaving', the overall transition state is 'out'.
Table
↓ Ancestor / Descendant →
'appearing'
'entering'
'in'
'leaving'
'out'
'appearing'
'appearing'
'appearing'
'appearing'
'leaving'
'out'
'entering'
'appearing'
'entering'
'entering'
'leaving'
'out'
'in'
'appearing'
'entering'
'in'
'leaving'
'out'
'leaving'
'leaving'
'leaving'
'leaving'
'leaving'
'out'
'out'
'out'
'out'
'out'
'out'
'out'
API
TransitionContext component
import { TransitionContext } from 'react-transition-context'
Props
state: TransitionState (optional)
The transition state of your transition component that is rendering this.
Omit this if you just want to consume the overall transition state without
changing the value for descendants.
children: React.Node (required)
The content to render
useTransitionContext hook
import { useTransitionContext } from 'react-transition-context'
Calls effect whenever the overall transition state from context changes
from 'out'/'leaving' to 'appearing', or is
'appearing' when the component mounts.
useAppearedEffect hook
import { useAppearedEffect } from 'react-transition-context'
Calls effect whenever the overall transition state from context changes
from 'out'/'leaving' to 'entering', or is
'entering' when the component mounts.
useEnteredEffect hook
import { useEnteredEffect } from 'react-transition-context'
Calls effect whenever the overall transition state from context changes
from 'in'/'appearing'/'entering' to 'leaving',
or when the component will unmount and the overall
transition state is 'in'/'appearing'/'entering'.
useLeftEffect hook
import { useLeftEffect } from 'react-transition-context'