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.

[Children] filter null values in map function

See original GitHub issue

Currently I am implementing some Dropdown component and I have something like this.

return (
  <div className={classnames(classes)} tabIndex="-1">
    {Children.map(children, this.render_child)}
  </div>
)

...

@autobind
render_child(element, index) {
  return cloneElement(element, {
    key: element.key || index,
    selected: this.props.selected,
    onClick: this.click_handler
  })
}

The business requirement put me in this situation. I created the list of item of the Dropdown component.

// this is using map from some array so I will return an array.
render_menu_item(key) {
  if(!key) { //whatever checking
    return
  }

  return (
    <MenuItem value={key}>{text}</MenuItem>
  )
}

Now, because I use element.key in some case element could be null so give me an error. I propose to remove the null or undefined from the Children.map because then I will be filtering all the time the null values whenever I have this situation.

I understand I can forEach but I think is not the best implementation, in the end you change the map function alright so

Issue Analytics

  • State:closed
  • Created 8 years ago
  • Comments:13 (3 by maintainers)

github_iconTop GitHub Comments

3reactions
sophiebitscommented, Sep 24, 2015

The new toArray removes nulls, btw.

2reactions
yordiscommented, Sep 19, 2015

@jimfb can you put me an example when the user want to have null? because people used a lot the null value for render nothing in some function base on some condition, the null or undefined values doesn’t add any value to the render, no?

Children is DOM/React element so I don’t see the problem with removing it by default like the react-dom does when you try to render a null value

Read more comments on GitHub >

github_iconTop Results From Across the Web

ReactJS children - filter out null values - Stack Overflow
You have to take advantage of Array.filter(): const MyComponent = ({ children }) => { // This filter will return only not null...
Read more >
[Solved]-ReactJS children - filter out null values-Reactjs
[Solved]-ReactJS children - filter out null values-Reactjs​​ toArray removes null values. children is not an array and cannot be treated as such (except...
Read more >
Remove Null Values when reducing to map
In my current mule project I am making a query against a data base and then getting an array containing all of the...
Read more >
TypeError: map() is not a function in React | bobbyhadz
The "TypeError: map is not a function" occurs when we call the map() method on a value that is not an array. To...
Read more >
Warning: Each Child in a List Should Have a Unique 'key' Prop
They just need to be unique across sibling elements. <ul> {["Item1", "Item2", "Item3"].map(item ...
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