Skip to content

Commit 0af932a

Browse files
committed
docs: add JSX rule about function references as children
Adds documentation explaining the common mistake of passing function references as JSX children instead of calling them. Includes examples showing incorrect and correct usage. Addresses facebook/react#34007
1 parent e9a7cb1 commit 0af932a

File tree

1 file changed

+27
-1
lines changed

1 file changed

+27
-1
lines changed

src/content/learn/writing-markup-with-jsx.md

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,32 @@ For historical reasons, [`aria-*`](https://developer.mozilla.org/docs/Web/Access
222222
223223
</Pitfall>
224224
225+
### 4. Don't pass function references as children {/*4-dont-pass-function-references-as-children*/}
226+
227+
JSX children should be elements, strings, or numbers—not function references. A common mistake is forgetting to call a function when you want to display its result:
228+
229+
```js
230+
// ❌ This doesn't work - displays [Function]
231+
export default function Button() {
232+
const handleClick = () => alert('Clicked!');
233+
return <button>{handleClick}</button>;
234+
}
235+
236+
// ✅ This works - call the function if you want its result
237+
export default function Greeting() {
238+
const getMessage = () => 'Hello, world!';
239+
return <div>{getMessage()}</div>;
240+
}
241+
242+
// ✅ Or pass it as a prop for event handling
243+
export default function Button() {
244+
const handleClick = () => alert('Clicked!');
245+
return <button onClick={handleClick}>Click me</button>;
246+
}
247+
```
248+
249+
If you accidentally pass a function reference as a child, React will display it as `[Function]` instead of rendering meaningful content.
250+
225251
### Pro-tip: Use a JSX Converter {/*pro-tip-use-a-jsx-converter*/}
226252
227253
Converting all these attributes in existing markup can be tedious! We recommend using a [converter](https://transform.tools/html-to-jsx) to translate your existing HTML and SVG to JSX. Converters are very useful in practice, but it's still worth understanding what is going on so that you can comfortably write JSX on your own.
@@ -350,4 +376,4 @@ export default function Bio() {
350376

351377
</Solution>
352378

353-
</Challenges>
379+
</Challenges>

0 commit comments

Comments
 (0)