I am following Chang Wang\'s tutorial for making reusable React transitions with HOCs and ReactTransitionGroup
(Part 1 Part 2) in conjunction with Huan Ji\'s tutoria
Imagine rendering the sample JSX below:
Foo
Bar
The
's children
prop would be made up of the elements:
[
{ type: 'div', props: { key: 'one', children: 'Foo' }},
{ type: 'div', props: { key: 'two', children: 'Bar' }}
]
The above elements will be stored as state.children
. Then, we update the
to:
Bar
Baz
When componentWillReceiveProps
is called, its nextProps.children
will be:
[
{ type: 'div', props: { key: 'two', children: 'Bar' }},
{ type: 'div', props: { key: 'three', children: 'Baz' }}
]
Comparing state.children
and nextProps.children
, we can determine that:
1.
{ type: 'div', props: { key: 'one', children: 'Foo' }}
is leaving
2.
{ type: 'div', props: { key: 'three', children: 'Baz' }}
is entering.
In a regular React application, this means that
would no longer be rendered, but that is not the case for the children of a
.
WorksSo how exactly is
able to continue rendering components that no longer exist in props.children
?
What
does is that it maintains a children
array in its state. Whenever the
receives new props, this array is updated by merging the current state.children
and the nextProps.children
. (The initial array is created in the constructor
using the initial children
prop).
Now, when the
renders, it renders every child in the state.children
array. After it has rendered, it calls performEnter
and performLeave
on any entering or leaving children. This in turn will perform the transitioning methods of the components.
After a leaving component's componentWillLeave
method (if it has one) has finished executing, it will remove itself from the state.children
array so that it no longer renders (assuming it didn't re-enter while it was leaving).
Now the question is, why aren't updated props being passed to the leaving element? Well, how would it receive props? Props are passed from a parent component to a child component. If you look at the example JSX above, you can see that the leaving element is in a detached state. It has no parent and it is only rendered because the
is storing it in its state
.
When you are attempting to inject the state to the children of your
through React.cloneElement
, the leaving component is not one of those children.
You can pass a childFactory
prop to your
. The default childFactory
just returns the child, but you can take a look at the
for a more advanced child factory.
You can inject the correct props into the children (even the leaving ones) through this child wrapper.
function childFactory(child) {
return React.cloneElement(child, {
transitionState,
dispatch
})
}
Usage:
var ConnectedTransitionGroup = connect(
store => ({
transitionState: state.transitions
}),
dispatch => ({ dispatch })
)(TransitionGroup)
render() {
return (
{children}
)
}
React Transition Group was somewhat recently split out of the main React repo and you can view its source code here. It is pretty straightforward to read through.