Is x-bind:class not evaluated on pageload/render/init? #2135
Unanswered
iantbutler01
asked this question in
1. Help
Replies: 1 comment 2 replies
-
No, x-bind is evaluated at the beginning as well like all the other directives. Likely |
Beta Was this translation helpful? Give feedback.
2 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
So I have a button, that using my backend's templating engine, receives some data. In this case that is 'sharing_enabled' which is boolean.
<button x-bind:class="{'bg-indigo-600': on == true, 'bg-gray-200': on == false}" x-data="{ on: <%= @sharing_enabled %> }" @click="on = !on" phx-click="toggle-sharing" type="button" class="bg-gray-200 relative inline-flex flex-shrink-0 h-6 w-11 border-2 border-transparent rounded-full cursor-pointer transition-colors ease-in-out duration-200 focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-indigo-500" role="switch" aria-checked="false">
The issue is on page load if sharing_enabled == true my background is not indigo, and it only changes if I force a state change by clicking. From this I'm gathering that x-bind:class isn't evaluated on page load and only when an event fires.
First, is that true? Then if it is, what would be the best approach to triggering that class change on pageload/render/init? Other than using my backend's templating engine to handle that, largely because it will be ugly and tedious for every component I have that will receive dynamic starting state.
Beta Was this translation helpful? Give feedback.
All reactions