Skip to content

docs: add JSX rule about function references as children #7908

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 27 additions & 1 deletion src/content/learn/writing-markup-with-jsx.md
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,32 @@ For historical reasons, [`aria-*`](https://developer.mozilla.org/docs/Web/Access

</Pitfall>

### 4. Don't pass function references as children {/*4-dont-pass-function-references-as-children*/}

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:

```js
// ❌ This doesn't work - displays [Function]
export default function Button() {
const handleClick = () => alert('Clicked!');
return <button>{handleClick}</button>;
}

// ✅ This works - call the function if you want its result
export default function Greeting() {
const getMessage = () => 'Hello, world!';
return <div>{getMessage()}</div>;
}

// ✅ Or pass it as a prop for event handling
export default function Button() {
const handleClick = () => alert('Clicked!');
return <button onClick={handleClick}>Click me</button>;
}
```

If you accidentally pass a function reference as a child, React will display it as `[Function]` instead of rendering meaningful content.

### Pro-tip: Use a JSX Converter {/*pro-tip-use-a-jsx-converter*/}

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.
Expand Down Expand Up @@ -350,4 +376,4 @@ export default function Bio() {

</Solution>

</Challenges>
</Challenges>