From 7cd4e2630b7bf2254e687c807e969059875cc3aa Mon Sep 17 00:00:00 2001 From: Brian Kim Date: Sat, 13 Jan 2024 11:18:44 -0800 Subject: [PATCH] pass context as the second argument to component --- src/crank.ts | 7 ++++++- test/sync-function.tsx | 14 ++++++++++++++ 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/src/crank.ts b/src/crank.ts index e5aa77785..dcfd84a99 100644 --- a/src/crank.ts +++ b/src/crank.ts @@ -148,6 +148,7 @@ export type Children = Child | ChildIterable; export type Component = any> = ( this: Context, props: TProps, + ctx: Context, ) => | Children | PromiseLike @@ -2365,7 +2366,11 @@ function runComponent( clearEventListeners(ctx); let result: ReturnType; 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; diff --git a/test/sync-function.tsx b/test/sync-function.tsx index df7e73f2b..d9ec163b4 100644 --- a/test/sync-function.tsx +++ b/test/sync-function.tsx @@ -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(, document.body); + Assert.is(ctx1, ctx2); +}); + test.run();