preactjs / preact-compat

ATTENTION: The React compatibility layer for Preact has moved to the main preact repo.
http://npm.im/preact-compat
MIT License
949 stars 148 forks source link

fix: children.map when new child return null #461

Open ishenli opened 6 years ago

ishenli commented 6 years ago

ref: https://github.com/developit/preact-compat/issues/419

When the new child is null, should ignore , like React

likezero commented 6 years ago

Hi,@developit can you merge this PR?

developit commented 6 years ago

I'm still confused how it's possible to end up with props.children containing values like null or undefined. Preact strips those out - see this demo: https://jsfiddle.net/developit/z97ua3m3/

So it would seem that, regardless of whether this PR is merged or not, Preact will still have removed the empty values prior to map() running, right?

Setting that aside, I think this could be simplified a bit? Something like:

    map(children, fn, ctx) {
        if (children == null) return null;
        children = Children.toArray(children);
        if (ctx) fn = fn.bind(ctx);
        return children.filter(Boolean).map(fn).filter(Boolean);
    }
maxsbelt commented 6 years ago

@developit children property can contain null values. I created simple example for preact@8.3.1: https://codesandbox.io/s/wwnx5q1277?expanddevtools=1