This is more or less my preferred way as well. However it is good to note that the component you pass as children will still be mounted (but not rendered) even if the condition is false. The only way to avoid the component being mounted is to have the condition and the component initialization in the same place.
functionAlwaysMounted({condition,children}:React.PropsWithChildren<{condition:boolean}>){return!!condition?children:null}constmyCondition=false// MyComponent will always be mounted (but not rendered)return<AlwaysMountedcondition={myCondition}><MyComponent/></AlwaysMounted>// This is the only way to not mount your component if the condition is falsereturn!!myCondition?<MyComponent/>:null
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
For my part, in all my projects, whether personal or professional, I create an If component with a condition as property
It allows me to have something easier to read in my code
This is more or less my preferred way as well. However it is good to note that the component you pass as children will still be mounted (but not rendered) even if the condition is false. The only way to avoid the component being mounted is to have the condition and the component initialization in the same place.