-
-
Notifications
You must be signed in to change notification settings - Fork 22
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
Inline if with nil pointer #152
Comments
Hi @onordgren , thank you for taking the time to file an issue! My answer time is a bit longer than usual, I've got the flu. 🤧 I agree that this is surprising behaviour, but actually a consequence of the design of gomponents itself. As you probably know, a component is basically (most often) a function. The functions are called inline in your example, and the component given to the I've explored possible solutions for this in #99 and #101, but haven't found a good one yet. Maybe I should use this opportunity to revisit the problem. Feedback and input appreciated. 😊 |
I tried an implementation for a solution, but I'm not entirely satisfied. If anyone wants to have a look and post their thoughts, they are very welcome! |
Hi, a new user of the library here! My humble suggestion is deprecating g.If and suggesting creating a function with an early nil return if the parameter does not satisfy. The language simply does not have the extensibility to enable any more elegant solution. But of course, I may be wrong and any pointers to alternative approaches would help a lot. |
Hi @egeozcan , thanks for chiming in. What you're proposing is essentially what the current implementation does: func If(condition bool, n Node) Node {
if condition {
return n
}
return nil
} The problem lies not in what the function does, but when parameters are evaluated when using it. Because gomponents is all just nested functions, the innermost function and its parameters is evaluated before we even reach the Or am I misunderstanding your suggestion? |
hi @markuswustenberg, well I meant not using a generic function, and using a custom function to do this. the g.node is super easy to compose so I humbly suggested that, that should be encouraged instead of offering abstractions that can't cover some cases such as the one discussed and may cause confusion. thanks for the detailed reply 😊 |
Hmm. Yeah, I get your point. I still think |
`Lazy` provides lazy evaluation of node rendering. Useful together with `If`. Fixes #152
@egeozcan @onordgren if you like and have the time, I'd be happy if you had a look at my proposed lazy evaluation solution at #156. I think I've finally arrived at an API I like. |
Finally landed on a satisfying solution in #172. |
Hello! Ran into the following issue today while trying gomponents. When using the inline if,
g.If
it seems like the node is evaluated even though the statement is false. Made a small example app below, where sessions
isnil
. There is an if statement checking if the session is not nil, and then proceeding to show the username. The code however panics with a nil reference error. Looking at the source code it would seem like the node wouldn't be evaluated, but maybe something else is going on?The text was updated successfully, but these errors were encountered: