Skip to content

Conversation

@deleonio
Copy link

@deleonio deleonio commented Jul 22, 2020

Fixes #2644 .

@deleonio
Copy link
Author

I still have to adjust the tests.

@deleonio
Copy link
Author

I think that is relevant too ... https://reactjs.org/docs/react-api.html#reactchildren

@coveralls
Copy link

coveralls commented Jul 28, 2020

Coverage Status

Coverage remained the same at 100.0% when pulling 8265ed8 on martinoppitz:fix/#2644 into 8603d70 on preactjs:master.

@deleonio
Copy link
Author

I hadn't seen that. I feel like a beginner.

image

@marvinhagemeister
Copy link
Member

No worries, like mentioned on twitter the this argument combined with Function.prototype.call() and Function.prototype.apply() is pretty esoteric these days and hardly used much anymore. So it's easy to miss 👍

expect(serializeHtml(scratch)).to.equal('<div></div>');
});

it('should propagate the this context', () => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a great start, but I think the commit isn't complete. The test defines a few functions but never calls them anywhere. This will make the test always pass, even against current master and without the changes in this PR.

Suggested change
it('should propagate the this context', () => {
it('should propagate this context', () => {
const context = {};
const spy = sinon.spy(child => child); // noop
const Foo = ({ children }) => {
return React.Children.map(children, spy, context);
};
render(<Foo>foo</Foo>, scratch);
expect(spy.thisValues[0]).to.equal(context);
});

return toChildArray(toChildArray(children).map(fn));
const mapFn = (children, fn, context) => {
if (!children) return null;
return toChildArray(children).reduce(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We don't generally use reduce(). I'd prefer to use .map(fn.bind(ctx)) for size and perf reasons.

@rschristian
Copy link
Member

Superseded by #3855

@rschristian rschristian closed this Aug 6, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Differences in Preact.Children API and React.Children API

5 participants