chore: add wrapper components for conditional rendering #4047
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Check out
Conditionals
in Storybook!ChooseOne
takes any number ofCond
children.Cond
wraps anything you want to render conditionally, plus acondition
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 lastcondition
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 acondition
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
return
s, or read everything in order to tell the difference between independent conditionals (Maybe
s) and interdependent conditionals (ChooseOne
s).To test them out, I put them in
TemplatesPageView
, where we had a lot of conditional rendering that was getting hard to read.