When to use bind vs hooks? #456
-
I'm confused about when should I use hooks than binds. Can someone also give a clear example. I refactored the setup hook of the simple bar example using binds and it's working as expected: const slider = Widget.Slider({
value: audio.speaker.bind('volume')
.as(v => v || 0),
hexpand: true,
draw_value: false,
on_change: ({ value }) => audio.speaker.volume = value,
// setup: self => self.hook(audio.speaker, () => {
// self.value = audio.speaker.volume || 0
// }),
}) |
Beta Was this translation helpful? Give feedback.
Answered by
Przegryw321
Jun 17, 2024
Replies: 1 comment
-
Use bind when you're just assigning a value, use hook when you need to do something more. Here's an example of my network widget, it sets the icon based on a function, but also controls the class to change the color of the icon. const network_status = Widget.Label({
className: 'bar-network',
setup: self => self.hook(network, self => {
self.label = get_network_icon(network)
self.toggleClassName('bar-network-disconnected', !network.primary)
self.toggleClassName(
'bar-network-connecting',
network.wired.internet == "connecting" || network.wifi.internet == "connecting"
)
})
}) |
Beta Was this translation helpful? Give feedback.
0 replies
Answer selected by
rafaeljacov
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Use bind when you're just assigning a value, use hook when you need to do something more.
Here's an example of my network widget, it sets the icon based on a function, but also controls the class to change the color of the icon.