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.
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.