listen for entering/leaving inside nested transitions
npm install react-transition-context




Helps you deal with nested transitions
```
npm install --save-dev react-transition-context
- Introduction
- TransitionState enum
- Overall transition state
- Table
- API
- TransitionContext component
- useTransitionContext hook
- useTransitionStateEffect hook
- useAppearingEffect hook
- useAppearedEffect hook
- useEnteringEffect hook
- useEnteredEffect hook
- useCameInEffect hook
- useLeavingEffect hook
- useLeftEffect hook
- useAutofocusRef hook
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/users
within the 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./users/:userId
So the 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/users/:userId
both put use it to put their transition state on React context, the form can register a effect to get called when it's fully
in (rather than mounted but appearing or entering).
enumThere are five possible values:
- 'appearing''entering'
- 'in'
- 'leaving'
- 'out'
-
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'.'leaving'
- If the ancestor is and the descendant is 'entering', the overall transition state is 'leaving'.'out'
- If the ancestor is and the descendant is 'leaving', the overall transition state is 'out'.
| ↓ 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' |
component`js`
import { TransitionContext } from 'react-transition-context'
#### 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
hook`js`
import { useTransitionContext } from 'react-transition-context'
Hook that returns the overall transition state from
context.
hook`js`
import { useTransitionStateEffect } from 'react-transition-context'
`js`
useTransitionStateEffect(
effect: (prevState: ?TransitionState, nextState: TransitionState) => any
)
Calls effect whenever the overall transition state from context changes.prevState will be null for the first call (on mount).
hook`js`
import { useAppearingEffect } from 'react-transition-context'
`js`
useAppearingEffect(
effect: (prevState: ?TransitionState, nextState: TransitionState) => any
)
Calls effect whenever the overall transition state from context changes'out'
from /'leaving' to 'appearing', or is'appearing' when the component mounts.
hook`js`
import { useAppearedEffect } from 'react-transition-context'
`js`
useAppearedEffect(
effect: (prevState: ?TransitionState, nextState: TransitionState) => any
)
Calls effect whenever the overall transition state from context changes'appearing'
from to 'in'.
hook`js`
import { useEnteringEffect } from 'react-transition-context'
`js`
useEnteringEffect(
effect: (prevState: ?TransitionState, nextState: TransitionState) => any
)
Calls effect whenever the overall transition state from context changes'out'
from /'leaving' to 'entering', or is'entering' when the component mounts.
hook`js`
import { useEnteredEffect } from 'react-transition-context'
`js`
useEnteredEffect(
effect: (prevState: ?TransitionState, nextState: TransitionState) => any
)
Calls effect whenever the overall transition state from context changes'entering'
from to 'in'.
hook`js`
import { useCameInEffect } from 'react-transition-context'
`js`
useCameInEffect(
effect: (prevState: ?TransitionState, nextState: TransitionState) => any
)
Calls effect whenever the overall transition state from context changes'in'
to (from any other state), or is 'in' when the
component mounts.
useAutofocusRef is built on
top of this.
hook`js`
import { useLeavingEffect } from 'react-transition-context'
`js`
useLeavingEffect(
effect: (prevState: ?TransitionState, nextState: TransitionState) => any
)
Calls effect whenever the overall transition state from context changes'in'
from /'appearing'/'entering' to 'leaving','in'
or when the component will unmount and the overall
transition state is /'appearing'/'entering'.
hook`js`
import { useLeftEffect } from 'react-transition-context'
`js`
useLeftEffect(
effect: (prevState: ?TransitionState, nextState: TransitionState) => any
)
Calls effect whenever the overall transition state from context changes'leaving'
from to 'out'.
hookCreates a ref you can pass to an element to automatically
focus it when the component comes in
`js
import * as React from 'react'
import { useAutofocusRef } from 'react-transition-context'
const LoginForm = () => {
const autofocusRef = useAutofocusRef()
return (
useTransitionStateEffectFilter`js
import { useTransitionStateEffectFilter } from 'react-transition-context'
``js
useTransitionStateEffectFilter: (
filter: (prevState: ?TransitionState, nextState: TransitionState) => boolean
) => (
effect: (prevState: ?TransitionState, nextState: TransitionState)
) => void
`A higher-order function that takes a filter function
and returns a transition state hook that only calls
effect when the filter returns truthy.(
useTransitionStateEffectFilter is used to create all
the useAppearingEffect, useLeavingEffect`, etc hooks)