Skip to content
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

fix(#2644): adapt the react children api #2645

Open
wants to merge 9 commits into
base: main
Choose a base branch
from

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 👍

@@ -99,6 +99,16 @@ describe('Children', () => {
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.

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
4 participants