Skip to content

Commit 55be7a2

Browse files
authored
Merge pull request #69 from solidjs-community/fix/fn-in-jsx
Treat inline functions as JSX children as tracked functions.
2 parents cce3fc1 + fcb56a6 commit 55be7a2

File tree

3 files changed

+22
-0
lines changed

3 files changed

+22
-0
lines changed

docs/reactivity.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -569,6 +569,17 @@ const m = createMemo(() => 5)!;
569569

570570
const m = createMemo(() => 5)! as Accessor<number>;
571571

572+
function Component(props) {
573+
return (
574+
<div>
575+
{() => {
576+
console.log("hello");
577+
return props.greeting;
578+
}}
579+
</div>
580+
);
581+
}
582+
572583
```
573584
<!-- AUTO-GENERATED-CONTENT:END -->
574585

src/rules/reactivity.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -840,6 +840,8 @@ const rule: TSESLint.RuleModule<MessageIds, []> = {
840840
// to the DOM. This is semantically a "called function", so it's fine to read reactive
841841
// variables here.
842842
pushTrackedScope(node.expression, "called-function");
843+
} else if (node.parent?.type === "JSXElement" && isFunctionNode(node.expression)) {
844+
pushTrackedScope(node.expression, "function"); // functions inline in JSX containers will be tracked
843845
} else {
844846
pushTrackedScope(node.expression, "expression");
845847
}

test/rules/reactivity.test.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -264,6 +264,15 @@ export const cases = run("reactivity", rule, {
264264
code: `const m = createMemo(() => 5)! as Accessor<number>;`,
265265
...tsOnlyTest,
266266
},
267+
// functions in JSXExpressionContainers
268+
`function Component(props) {
269+
return (
270+
<div>{() => {
271+
console.log('hello');
272+
return props.greeting;
273+
}}</div>
274+
);
275+
}`,
267276
],
268277
invalid: [
269278
// Untracked signals

0 commit comments

Comments
 (0)