Interactivity API how to use data-wp-bind--checked in a loop? #63595
Replies: 1 comment 1 reply
-
This directive is binding the value in state to the attribute The result is that all of the inputs have the attribute You'll want to use derived state to bind the checked value to a boolean like this: const { state } = store( "example", {
state: {
get isChecked() {
return getContext().answer === state.chosen;
},
},
} ); And bind the checked value accordingly: A few other notes, it's generally preferable to set state on the server side with However in this case it may be preferable to use only context. State is global (all interactive regions on the page share the same state) whereas context is local to the specific region. If this is a block and its rendered multiple times, changing the radio in any of the blocks would update the selected item in all of the blocks. |
Beta Was this translation helpful? Give feedback.
-
Is it possible to bind checked on a group of radio buttons generated in a loop and have the checked state be determined by the state?
Strangely enough in my current setup the last value is checked although 2 is in the state.
If I click on a value it is correctly set in the state and input field but not the right checkbox shows checked. You need to click twice to have it look checked...
Except when choosing 2 which is similar to the state value.
I am puzzled.
The behavior changes depending on using strings or integers??? '1' versus 1
Is this a timing or rerender issue?
Is bind even supported?
IMHO it should be.
How to get this working?
view.js:
render.php:
Beta Was this translation helpful? Give feedback.
All reactions