-
-
Notifications
You must be signed in to change notification settings - Fork 684
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
[Feature]: Rethink Type Safety and Flexibility of the view!
Macro for Conditional Logic
#3378
Comments
Are you interested in working on a PR? |
Absolutely! I'd love to contribute to this framework and help make it easier for everyone to build UIs in Leptos and beyond. However, I'll need some guidance on where to start implementing this feature since there are too many crates in this repository. But, I will have a look and study this repo crates over the weekend. Looking forward to your guidance! |
The |
I agree that it's a bit cumbersome if you have lots of conditionals in the view macro. Here are some tips from my (short) experience working with the leptos view macro: if connected.get() {
Some(view!{ ... })
} else {
None
} could be condensed a bit by using bool then: connected.get().then(|| view!{ ... }) and for if let Some(key) = phantom_wallet_adapter.get().public_key() {
view!{ ... }.into_any()
} else if let Some(key) = solflare_wallet_adapter.get().public_key() {
view!{ ... }.into_any()
} else {
view!{ ... }.into_any()
} the either macro could be used, which takes care of wrapping each arm in an Either* variant either!(
(Some(1), Some(2)),
(Some(a), Some(b)) => view! {},
(Some(a), None) => view! {},
(None, Some(b)) => view! {},
_ => view! {},
) |
Thanks for sharing these tips, @geovie! However, this doesn't fully address the main issues outlined in the issue. I believe there's an opportunity to make it easier to build logic within the |
Hiya!
I've been using Leptos for a while now (ever since building this crate), and I've noticed that working with the
view!
macro can be quite cumbersome when adding conditional logic (if-else
) inside the macro. Let me explain with some examples:The Problem
To add
if-else
logic inview!
, it always has to be wrapped in a closure ({move ||}
). Here's a complex example:This setup becomes messy and hard to read or maintain when handling complex structures.
{move || }
.if-else
must use the same HTML tag type, making it impossible to render different tags (e.g.,<input>
in one branch and<textarea>
in the other).The above fails because the
class
attribute types differ (&str
vs.String
).Real-World Frustration
When I was building a crate to simplify input components for WASM frameworks, I found these limitations made it frustrating to handle dynamic, conditional logic while keeping the code clean and maintainable.
Suggestion
To improve usability and maintainability of the
view!
macro:if-else
if optional, when wrapped withSome()
.{move ||}
closures to simplify writing dynamic logic.This would make it far easier to write concise and readable logic inside
view!
, especially for more complex use cases.Thanks for considering this! 😊
Best!
The text was updated successfully, but these errors were encountered: