React.Child.isNull - Ability to check if a component rendered null.
See original GitHub issueSuppose I want a component Concatenate
that concatenates it’s children together using a separator prop…
import React from 'react';
import _ from 'lodash';
class Concatenate extends React.Component {
render() {
var children = React.Children.toArray(this.props.children);
var separated = children.reduce((previous, current, index) => {
previous.push(current);
if (index !== children.length - 1) {
previous.push(this.props.separator(index));
}
return previous;
}, []);
return <div {...this.props}>{separated}</div>;
}
}
Concatenate.defaultProps = {
separator: (key) => {
return <span key={key}> ⋅ </span>;
}
};
export default Concatenate;
I would like to choose to not render a separator if the corresponding child decides to render null, is this possible? Is there a React.Child.isNull functionality?
Issue Analytics
- State:
- Created 8 years ago
- Reactions:27
- Comments:5 (2 by maintainers)
Top Results From Across the Web
React component children detect if empty / null before render
Solution 1: Get the children type . It will return null if Child component returns null: const isChildNull = children => { return ......
Read more >Stop returning NULL components - DEV Community
Stop returning NULL components ... Conditional rendering on React helps you build your apps avoiding unnecessary renders depending on some ...
Read more >Conditional rendering vs. returning null : r/reactjs - Reddit
Hey there. I'd like to hear some opinions on conditional rendering vs. returning content/null from a component.
Read more >Match - React Router: Declarative Routing for React.js
When this is the case, the match will be null . Being able to render a <Route> 's contents when it does match...
Read more >React Top-Level API
React also provides a component for rendering multiple elements without a wrapper. ... If children is null or undefined , this method will...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
(The experimental React Call Return API has been removed)
No and unfortunately this won’t be possible - proptypes are done early on the elements and don’t have access to the rendered result.