Skip to content
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

Handle "False" child elements #40

Closed
wants to merge 1 commit into from
Closed

Handle "False" child elements #40

wants to merge 1 commit into from

Conversation

ggranger
Copy link

@ggranger ggranger commented Aug 1, 2024

I'm submitting this tiny change to improve conditional rendering.

Currently, only "None" nodes are skipped.
This is useful to implement conditional rendering based on a nullable variable, as shown in the docs.

print(div[error and b[error]])

Unfortunately, most conditional rendering often relies on a boolean condition, and in this case, you can't do print(div[has_error and b[error_msg]]) because you will get an error ValueError: False is not a valid child element

The current best way to have conditional rendering based on a boolean is to adopt the ternary operator print(div[b[error_msg] if has_error else None]).

Drawbacks are:

  • condition appears after the (often very long) block, making it hard to read
  • you still have to have to write the else None part all the time, which is tedious

The suggested change aims to allow the print(div[has_error and b[error_msg]]) syntax for boolean conditional rendering.

@pelme
Copy link
Owner

pelme commented Aug 1, 2024

Hi @ggranger, thanks for bringing this up. There have been a bit of back and forth on this. I agree that this is nice. I did not previously know how to deal with typing and True. I looked into what React does. React just ignores True. So.. having a way to deal with True which makes it possible to add bool to the Node type. I created #41 with updated docs+types+tests, please have a look at it!

@ggranger
Copy link
Author

ggranger commented Aug 2, 2024

Thanks for that and congrats for adding the True value as well as part of the ignored values which is helpful to handle the or construct in addition to the and one (I left a comment on your PR regarding that part).

@pelme pelme closed this in #41 Aug 3, 2024
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.

2 participants