Comments (7)
Hmm. Yeah, I get your point. I still think If
has value, though, because it's so easy to inline components conditionally that would otherwise need to be branched elsewhere. Alternatively, one can use the IIFE pattern from Javascript-land (https://en.wikipedia.org/wiki/Immediately_invoked_function_expression).
from gomponents.
@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.
from gomponents.
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 g.If
is always evaluated, but the result may be discarded. That's the reason for your nil pointer error.
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. 😊
from gomponents.
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!
from gomponents.
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.
from gomponents.
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 If
, and that's where the nil panics occur. The only way around that is to guard against nil in a custom inline function (which returns a Node
), or check for nils earlier in the component and behave appropriately.
Or am I misunderstanding your suggestion?
from gomponents.
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 😊
from gomponents.
Related Issues (20)
- Fragments HOT 6
- Links? HOT 1
- Why g.Map doesn't also do g.Group? HOT 2
- Generics g.Map performance HOT 7
- Use Custom attributes & alpine js HOT 3
- component specific css and js HOT 6
- Use with tinygo HOT 3
- Merge multiple occurrences of the `class` attribute HOT 5
- Proposal: Re-export packages with shorter names (g, h, c) HOT 3
- Why can't a group be rendered? HOT 5
- Add lib to awesome Go HOT 1
- Remove fmt.Stringer support? HOT 1
- Handling internationalization HOT 8
- trigger an error on duplicate attributes HOT 3
- Make Group renderable
- Proposal to Add String Rendering Method to Node Interface HOT 5
- HTML -> gomponents converter tool HOT 2
- Label can be an attribute as well HOT 2
- Missing list attribute HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from gomponents.