Skip to content

Commit

Permalink
pass context as the second argument to component
Browse files Browse the repository at this point in the history
  • Loading branch information
Brian Kim committed Jan 13, 2024
1 parent 0d82326 commit 7cd4e26
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 1 deletion.
7 changes: 6 additions & 1 deletion src/crank.ts
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,7 @@ export type Children = Child | ChildIterable;
export type Component<TProps extends Record<string, unknown> = any> = (
this: Context<TProps>,
props: TProps,
ctx: Context<TProps>,
) =>
| Children
| PromiseLike<Children>
Expand Down Expand Up @@ -2365,7 +2366,11 @@ function runComponent<TNode, TResult>(
clearEventListeners(ctx);
let result: ReturnType<Component>;
try {
result = (ret.el.tag as Component).call(ctx.owner, ret.el.props);
result = (ret.el.tag as Component).call(
ctx.owner,
ret.el.props,
ctx.owner,
);
} catch (err) {
ctx.f |= IsErrored;
throw err;
Expand Down
14 changes: 14 additions & 0 deletions test/sync-function.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -104,4 +104,18 @@ test("refresh called on unmounted component", () => {
}
});

test("context is passed as second argument", () => {
let ctx1!: Context;
let ctx2!: Context;

function Component(this: Context, _props: any, ctx: Context) {
ctx1 = this;
ctx2 = ctx;
return null;
}

renderer.render(<Component />, document.body);
Assert.is(ctx1, ctx2);
});

test.run();

0 comments on commit 7cd4e26

Please sign in to comment.