Skip to content

Commit 7cd4e26

Browse files
author
Brian Kim
committed
pass context as the second argument to component
1 parent 0d82326 commit 7cd4e26

File tree

2 files changed

+20
-1
lines changed

2 files changed

+20
-1
lines changed

src/crank.ts

+6-1
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,7 @@ export type Children = Child | ChildIterable;
148148
export type Component<TProps extends Record<string, unknown> = any> = (
149149
this: Context<TProps>,
150150
props: TProps,
151+
ctx: Context<TProps>,
151152
) =>
152153
| Children
153154
| PromiseLike<Children>
@@ -2365,7 +2366,11 @@ function runComponent<TNode, TResult>(
23652366
clearEventListeners(ctx);
23662367
let result: ReturnType<Component>;
23672368
try {
2368-
result = (ret.el.tag as Component).call(ctx.owner, ret.el.props);
2369+
result = (ret.el.tag as Component).call(
2370+
ctx.owner,
2371+
ret.el.props,
2372+
ctx.owner,
2373+
);
23692374
} catch (err) {
23702375
ctx.f |= IsErrored;
23712376
throw err;

test/sync-function.tsx

+14
Original file line numberDiff line numberDiff line change
@@ -104,4 +104,18 @@ test("refresh called on unmounted component", () => {
104104
}
105105
});
106106

107+
test("context is passed as second argument", () => {
108+
let ctx1!: Context;
109+
let ctx2!: Context;
110+
111+
function Component(this: Context, _props: any, ctx: Context) {
112+
ctx1 = this;
113+
ctx2 = ctx;
114+
return null;
115+
}
116+
117+
renderer.render(<Component />, document.body);
118+
Assert.is(ctx1, ctx2);
119+
});
120+
107121
test.run();

0 commit comments

Comments
 (0)