[Children] filter null values in map function
See original GitHub issueCurrently 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:
- Created 8 years ago
- Comments:13 (3 by maintainers)
Top 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 >
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 new toArray removes nulls, btw.
@jimfb can you put me an example when the user want to have
null
? because people used a lot thenull
value for render nothing in some function base on some condition, thenull
orundefined
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 thereact-dom
does when you try to render anull
value