Skip to content

Commit

Permalink
make sure refs work with hydration
Browse files Browse the repository at this point in the history
  • Loading branch information
Brian Kim committed Feb 4, 2024
1 parent b5e1947 commit 89238ac
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 0 deletions.
3 changes: 3 additions & 0 deletions src/crank.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1216,6 +1216,9 @@ function commitHost<TNode, TScope>(
let value = ret.value as TNode;
if (hydrationValue != null) {
value = ret.value = hydrationValue;
if (typeof ret.el.ref === "function") {
ret.el.ref(value);
}
}

let props = ret.el.props;
Expand Down
9 changes: 9 additions & 0 deletions test/refs.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -186,4 +186,13 @@ test("transcluded in async function component", async () => {
Assert.is(fn.lastCall.args[0], document.body.firstChild!.firstChild);
});

test("it works with hydrate", async () => {
const fn = Sinon.fake();
renderer.hydrate(<div ref={fn}>Hello</div>, document.body);
Assert.is(document.body.innerHTML, "<div>Hello</div>");
const div = document.body.firstChild;
Assert.is(fn.callCount, 1);
Assert.is(fn.lastCall.args[0], div);
});

test.run();

0 comments on commit 89238ac

Please sign in to comment.