Skip to content

Conditional Components #13763

Closed
Closed
@seanyriver

Description

@seanyriver

Feature

What is the current behavior?

Often we will use expressions like this:

{ !x ? null : <Component title={x.title} /> }

What is the desired behavior?

We would like to have expressive components such as

<When c={x}>
   <Component title={x.title} />
</When>

In many cases this generic syntax is preferred over an explicit child component or pure function which knows about what it is supposed to render.

The issue is that this will evaluate the children, even if they ultimately are not returned (since they are passed as children to the When component).

What is desired is for the syntax above to be able to behave exactly like a conditional expression, in that the contained children are not actually processed at all unless a condition is met. In reality the component could be something else entirely such as <SuperUser /> which will only process and render the children if the current user is a super user, or <Morning /> to only process and render items between 8 AM and 11 AM (or similar). The key thing is that we do not want the props passed into the children, or the children themselves produced, unless a condition has been satisfied.

Which versions of React, and which browser / OS are affected by this issue? Did this work in previous versions of React?

Presumably any version of React.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions