Thanks to visit codestin.com
Credit goes to github.com

Skip to content

chore: add wrapper components for conditional rendering #4047

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Sep 14, 2022

Conversation

presleyp
Copy link
Contributor

Check out Conditionals in Storybook!

ChooseOne takes any number of Cond children. Cond wraps anything you want to render conditionally, plus a condition prop; it's just a way of attaching a condition to some JSX. ChooseOne renders exactly one of its children: the first one whose condition is true, or else the last one. The last condition is ignored, but I didn't make it an optional prop because I think it would lead to worse bugs if people forgot to add it in other cases. So I suggest you just write the last one <Cond condition> so it's obvious that it's not conditioned on anything.

Maybe takes a condition prop and a child. It renders its child if the condition is true; otherwise it returns null.

My hope is that these will make our render code more readable because we don't have to break it up with curly braces, juggle ternaries, rearrange returns, or read everything in order to tell the difference between independent conditionals (Maybes) and interdependent conditionals (ChooseOnes).

To test them out, I put them in TemplatesPageView, where we had a lot of conditional rendering that was getting hard to read.

@presleyp presleyp requested a review from a team as a code owner September 13, 2022 20:52
@presleyp presleyp requested review from BrunoQuaresma and Kira-Pilot and removed request for a team September 13, 2022 20:52
</TableCell>
</TableRow>
</Cond>
<Cond condition>
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why condition here does not have a value?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The last Cond is the fallback, like in a switch statement - if the other conditions are not met, this will render regardless of whether its condition is true. So I thought the best convention would be to write it this way so that the reader knows it's not a real condition. The alternative is to make condition optional and omit it here, but if it were accidentally omitted in an earlier Cond, that one would always be skipped, and I thought avoiding that bug was important.

Copy link
Collaborator

@BrunoQuaresma BrunoQuaresma left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Really good addition! Just a minor comment.

@presleyp presleyp merged commit b6712ff into main Sep 14, 2022
@presleyp presleyp deleted the presleyp/conditional branch September 14, 2022 13:55
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants