Best practice in React components: multiple returns vs single return with conditional content? #5186
Replies: 2 comments 2 replies
-
|
There's no real best practice here. Components are the same as any other function. Multiple returns are valid. Sometimes they simplify things (handling a loading or error state first, which makes the rest of the code shorter or enabling TS to infer that a |
Beta Was this translation helpful? Give feedback.
-
|
in this case the single return makes sense because no matter the content, you want it wrapped in the section element. using a variable and single return avoids that duplication. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
Hi everyone 👋
I’d like to get some guidance / best-practice clarification regarding component structure in React, especially in the context of RTK Query usage.
In the RTK Query docs and examples, I see that some components are written using a single return, with conditional logic that assigns JSX to a variable (e.g. content) before returning it, like here is the second example of this section.
Example (simplified):
An alternative pattern that many React developers use is early returns / multiple returns, for example:
Questions
Is there a recommended or preferred pattern between:
returnwith conditional JSX assignment?Are there any readability, maintainability, or performance considerations that justify choosing one approach over the other?
Is the single-return style used in RTK Query examples mainly for teaching/consistency purposes, or is it considered a best practice?
I’m particularly interested in guidance for production codebases and team conventions.
I’m particularly interested in guidance for production codebases and team conventions.
Thanks in advance for your insights 🙏
Beta Was this translation helpful? Give feedback.
All reactions