You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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
Copy file name to clipboardExpand all lines: src/content/learn/writing-markup-with-jsx.md
+27-1Lines changed: 27 additions & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -222,6 +222,32 @@ For historical reasons, [`aria-*`](https://developer.mozilla.org/docs/Web/Access
222
222
223
223
</Pitfall>
224
224
225
+
### 4. Don'tpassfunction 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 functionButton() {
232
+
consthandleClick= () =>alert('Clicked!');
233
+
return<button>{handleClick}</button>;
234
+
}
235
+
236
+
// ✅ This works - call the function if you want its result
If you accidentally pass a function reference as a child, React will display it as `[Function]` instead of rendering meaningful content.
250
+
225
251
### Pro-tip: Use a JSX Converter {/*pro-tip-use-a-jsx-converter*/}
226
252
227
253
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() {
0 commit comments