Avoiding Msg types #2660
Replies: 3 comments
-
|
This is a valid pattern and the function component used to use this approach with a boxed function being the message type. However, If a boxed function is used as the message type, it will incur an allocation every time a message is sent to the component. In addition, boxed functions are dynamically dispatched, which means the compiler will not be able to apply most optimisation. This would result in:
|
Beta Was this translation helpful? Give feedback.
-
|
Another advantage to the long-hand approach is predictable behavior. There's one location where messages are handled, so only one place to look for side-effects. A little more typing isn't all that bad a trade-off for greater control. And we always have functional components for people who don't care for that level of granularity. |
Beta Was this translation helpful? Give feedback.
-
|
I see. Many thanks for your answers! |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
I noticed that Component Msg types are quite a lot of typing, and I wonder why we need it at all.
Instead, we could simply send a closure like:
The update function would be as simple as:
One could use that inside the view function:
Are there disadvantages of such approach?
Beta Was this translation helpful? Give feedback.
All reactions