question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

React.Child.isNull - Ability to check if a component rendered null.

See original GitHub issue

Suppose 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}> &sdot; </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:closed
  • Created 8 years ago
  • Reactions:27
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

6reactions
damassicommented, Oct 9, 2018

(The experimental React Call Return API has been removed)

1reaction
zpaocommented, Nov 20, 2015

No and unfortunately this won’t be possible - proptypes are done early on the elements and don’t have access to the rendered result.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found