-
Notifications
You must be signed in to change notification settings - Fork 48.6k
[Children] filter null values in map function #4867
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
this is soft of related to #2956 |
There are reasonable situations where users might want to have the nulls when mapping over elements, and it seems bad for map to skip elements. Implementing filter as mentioned in #2956 would be the better solution, if we were to do such a thing, but filter does have its own set of issues as mentioned in the bug. Specifically, the effects on reconciliation performance. Closing as a duplicate of #2956, since that solves this use case and is tracking the use case. |
@jimfb can you put me an example when the user want to have
|
The new toArray removes nulls, btw. |
@yordis See syranide's comment: #2956 (comment) Basically, by preserving the nulls in the array, you preserve the implicit keys that React uses to track instance identity. This ensures that the state of stateful components gets properly preserved when items are added/removed from the list. It's a fairly advanced topic, but it's a valid use case. The fact that nulls aren't removed from map means it "accidentally" works more often for people who aren't familiar with how implicit keys work. |
@jimfb That's not a real concern since things get rekeyed by React.Children.map. |
@spicyj thanks a lot about the |
@spicyj Since React.Children.map rekeys, do we want to reopen? My intuition is we should retain nulls, since that's how array.map works and I don't see adding a null-check as that burdensome, but I did close under the understanding that retaining nulls was advantageous for reconciliation but apparently that's not necessary, so I'm fine with reopening if you think this is something we might do. |
Sure, we can. It's hard to imagine what could break. I guess it would be kind of weird if you have a component like |
@spicyj In my situation I cloned the children and because I have <div>{this.renderItem()}</div> // could be `null` or an element.
renderItem() {
if(anyCheckingIsTrue) {
return <h1>Great</h1>
}
} // This is a map that give you some `null` or `undefined` values
let list = this.filters()
// and then
<Test>{list}</Test>
class Test extends from Component {
render() {
return (
<div>
{Children.map(this.props.children, this.renderItems)}
</div>
)
}
renderItems(element, index) {
// we want to do something with that element
// this is clean in my opinion, but I have to do the check
return cloneElement(element, {
key: element.key || index, // this will fail if we dont put the check when element is null
onClick: this.itemClick
...
})
}
} |
I am agree to @jimfb that 'null' items are valid and should not be taken out through Children.map function. This is not what map function should do. We have Children.map function for looping though a specific Children list and apply a function against each child. So, map is not responsible of rendering something but formatting children list. Developers (reactjs users) may need to render null children differently other than not null children . I mean null does't always mean "not to be rendered" but may be "rendered differently".
In my opinion is that, this is not a bug and "works as intended" . Web Developers should take this account and design their solutions accordingly. For example for the initial snippet from @yordis ,
|
I agree there is value in removing nulls, but In general we don't see Closing this. |
Sorry for the resurrection but, how is createContext an alternative to children inspection? |
Currently I am implementing some
Dropdown
component and I have something like this.The business requirement put me in this situation. I created the list of item of the
Dropdown
component.Now, because I use
element.key
in some caseelement
could benull
so give me an error. I propose to remove thenull
orundefined
from theChildren.map
because then I will be filtering all the time thenull
values whenever I have this situation.I understand I can
forEach
but I think is not the best implementation, in the end you change themap
function alright soThe text was updated successfully, but these errors were encountered: