ReactTransitionGroup: Rendering a Single Child
Do you want to request a feature or report a bug? Bug
What is the current behavior? In the "Rendering a Single Child" FB documentation / readme, coders are encouraged to pass in prop component = null for ReactTransitionGroup like so if childrenArray[0] is not truthy:
function FirstChild(props) {
const childrenArray = React.Children.toArray(props.children);
return childrenArray[0] || null;
}
<ReactTransitionGroup component={FirstChild}>
{someCondition ? <MyComponent /> : null}
</ReactTransitionGroup>
However, if null is passed in as component, e.g.,
var ReactTransitionGroup = React.addons.TransitionGroup;
var Hello = React.createClass({
render: function() {
return <ReactTransitionGroup component={null}/>;
}
});
ReactDOM.render(
<Hello />,
document.getElementById('container')
);
The following console error is thrown:
Warning: React.createElement: type is invalid -- expected a string (for built-in components) or a class/function (for composite components) but got: null. Check the render method of `ReactTransitionGroup`.
printWarning @ npmcdn.com/[email protected]/dist/react-with-addons.js:5276
npmcdn.com/[email protected]/dist/react-dom.js:17903 Uncaught Error: Element type is invalid: expected a string (for built-in components) or a class/function (for composite components) but got: null. Check the render method of `ReactTransitionGroup`.
at invariant (npmcdn.com/[email protected]/dist/react-dom.js:17896)
at ReactCompositeComponentWrapper.instantiateReactComponent [as _instantiateReactComponent] (npmcdn.com/[email protected]/dist/react-dom.js:16037)
at ReactCompositeComponentWrapper.performInitialMount (npmcdn.com/[email protected]/dist/react-dom.js:4821)
at ReactCompositeComponentWrapper.mountComponent (npmcdn.com/[email protected]/dist/react-dom.js:4712)
at Object.mountComponent (npmcdn.com/[email protected]/dist/react-dom.js:11542)
at ReactCompositeComponentWrapper.performInitialMount (npmcdn.com/[email protected]/dist/react-dom.js:4825)
at ReactCompositeComponentWrapper.mountComponent (npmcdn.com/[email protected]/dist/react-dom.js:4712)
at Object.mountComponent (npmcdn.com/[email protected]/dist/react-dom.js:11542)
at ReactCompositeComponentWrapper.performInitialMount (npmcdn.com/[email protected]/dist/react-dom.js:4825)
at ReactCompositeComponentWrapper.mountComponent (npmcdn.com/[email protected]/dist/react-dom.js:4712)
If the current behavior is a bug, please provide the steps to reproduce and if possible a minimal demo of the problem via https://jsfiddle.net or similar. See https://jsfiddle.net/urLq585z/ and open your console.
What is the expected behavior? No error should be thrown when null is passed as the component prop to ReactTransitionGroup. Nothing should render. I'd also like to point out that if you don't explicitly set component to null, e.g., render <ReactTransitionGroup /> the error is not present the default component prop is a span tag.
Which versions of React, and which browser / OS are affected by this issue? Did this work in previous versions of React? Latest version of React; I am on Chrome 56 on MacOS. Unsure about previous versions; presumably also broken.
(originally reported at https://github.com/facebook/react/issues/9056)
Hey @petermikitsh sorry for the delay responding. You aren't actually following the example correctly here. component cannot be null, but it can render null. See the above. FirstChild is actually a react component that renders nothing, or it's first child. This si different than setting the component to null! It's a pretty confusing suggestion i'll admit. Myself I have a hard time understanding why it works without really thinking about it :P
@jquense Can you explain please? I cannot understand that option:
Do you have an example with code? (easy to understand)
@hmontes Trying to help clarify here. There is a difference between:
function FirstChild(props) {
const childrenArray = React.Children.toArray(props.children);
return childrenArray[0] || null;
}
<ReactTransitionGroup component={FirstChild}>
{someCondition ? <MyComponent /> : null}
</ReactTransitionGroup>
and
<ReactTransitionGroup component={null}>
{someCondition ? <MyComponent /> : null}
</ReactTransitionGroup>
In the first example, the provided component prop is function -- and more precisely, a stateless, functional React component.
In the second example, you're simply passing in null.
The API for ReactTransitionGroup expects the component prop to be, well, a component. So the error I was seeing and reported here is the expected behavior. null isn't a component.
And to further clarify, and this is probably what tripped me up at the time, is that components can render null.
So if you declare FirstChild like so:
function FirstChild() {
return null;
}
You can pass that in to ReactTransitionGroup as the component prop since it's a valid React component.
@jquense Have you considered clarifying this in the documentation? I'd open a PR, but I don't think those docs are public-facing? We made the same mistake of using <TransitionGroup component={null} />. The docs are fairly misleading.
@solonaarmstrong I would love a PR. What do you mean by public-facing? Those docs are being deployed on each release, I think.
I'm talking about the docs in the www directory, not Storybook.