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
I have searched the existing issues and my issue is unique
My issue appears in the command-line and not only in the text editor
Description Overview
Conditionally rendering a string value with extra braces are hard to spot and can cause a runtime error.
Should jsx-no-leaked-render cover also return values?
typeProps={isInteractive: booleanlabel: string}constTest: React.FunctionComponent<Props>=({ isInteractive, label })=>(<div>{/* Will render `{label}` (object) instead of a string when `isInteractive` is false */}{isInteractive
? <button>{label}</button>
: {label}}{isInteractive&&<button>{label}</button>}{/* ...same issue as above */}{!isInteractive&&{label}}</div>)
When isInteractive is false the code will return an object instead of a string, but it's easy to miss in the ternary case. The error is Uncaught Error: Objects are not valid as a React child (found: object with keys {label}). If you meant to render a collection of children, use an array instead.
Appears both in VS Code Eslint plugin (dbaeumer.vscode-eslint) and command line running npx eslint . with following config:
@ljharb I believe Typescript compiler can't guard against this issue because the React.FunctionComponent return type is ReactElement<any, any> | null and it can contain nested components, jsx elements and plaintext.
If we focus on the conditional, the issue is that one can accidentally return an object instead of evaluated string. So if the component is called with <Test label="Frank" isInteractive={false} />
{ !isInteractive && label } – ts return value evaluates to string, Frank { !isInteractive && {label} } – ts return value evaluates to object, {label: "Frank"}, and it will cause this error.
This case is still quite easy to spot, but it took a long time for me to understand the issue when using a ternary. It would be great if Eslint could spot this issue when Typescript can't.
(I just realised my naming of the issue is a bit silly as both left and right side of the conditional can become the component return value. If I read correctly jsx-no-leaked-render rule covers only the left side currently.)
Is there an existing issue for this?
Description Overview
Conditionally rendering a string value with extra braces are hard to spot and can cause a runtime error.
Should jsx-no-leaked-render cover also return values?
When
isInteractive
is false the code will return an object instead of a string, but it's easy to miss in the ternary case. The error is Uncaught Error: Objects are not valid as a React child (found: object with keys {label}). If you meant to render a collection of children, use an array instead.Appears both in VS Code Eslint plugin (dbaeumer.vscode-eslint) and command line running
npx eslint .
with following config:Expected Behavior
Report extraneous braces around strings on conditional renders. Remove extra braces on fix.
eslint-plugin-react version
v7.33.2
eslint version
v8.47.0
node version
v20.2.0
The text was updated successfully, but these errors were encountered: