Seeking clarity and more understanding of the Interactive API #59677
Replies: 2 comments 3 replies
-
The closest mental model is to think about the Interactivity API as a (P)React application instead of as vanilla JS or jQuery. For example, you should never mutate the DOM directly in React: const Button = () => {
const ref = useRef();
const addClass = () => {
ref.classList.add("some-class");
};
return <button onClick={addClass}>Add class</button>;
}; Instead of that, you should create a state that you can manipulate, and then use the existing primitives to link the DOM to that state. const Button = () => {
const [hasSomeClass, setHasSomeClass] = useState(false);
const addClass = () => {
setHasSomeClass(true);
};
return (
<button className={hasSomeClass ? "some-class" : ""} onClick={addClass}>
Add class
</button>
);
}; Similarly, when using the Interactivity API, you should never mutate the DOM directly: <button data-wp-on--click="actions.addClass">Add class</button> store("myPlugin", {
actions: {
addClass() {
const { ref } = getElement();
ref.classList.add("some-class");
},
},
}); Instead, you should create a state and link the class name to the state. <button
data-wp-class--some-class="state.hasSomeClass"
data-wp-on--click="actions.addClass"
>
Add class
</button> const { state } = store("myPlugin", {
state: {
hasSomeClass: false,
},
actions: {
addClass() {
state.hasSomeClass = true;
},
},
}); That declarative approach might seem slightly more complex for very simple things like this, but it gets much much simpler when the UI starts getting complex.
You should avoid using jQuery in conjunction with the Interactivity API, in the same way that nobody uses jQuery in conjunction with (P)React. Can you provide the link to the documentation page you mentioned? It's probably worth updating it as nowadays, jQuery is not recommended anymore.
As I said, you should never manipulate the DOM directly. Always use the provided event listeners instead (
For localization, there's a simpler way: you can store the translated strings in the state, using <?php
$add_class_text = __( 'Add class' );
$remove_class_text = __( 'Remove class' );
wp_interactivity_state( "myPlugin", array(
'hasSomeClass': false,
'addClassText': $add_class_text,
'removeClassText': $remove_class_text,
'buttonText': $add_class_text, // initial value
));
?>
<div data-wp-interactive="myPlugin">
<button
data-wp-class--some-class="state.hasSomeClass"
data-wp-on--click="actions.toggleClass"
data-wp-text="state.buttonText"
></button>
</div> const { state } = store("myPlugin", {
state: {
hasSomeClass: false,
get buttonText() {
// Change the text of the button dynamically depending on the state.
return state.hasSomeClass ? state.removeClassText : state.addClassText;
},
},
actions: {
toggleClass() {
state.hasSomeClass = !state.hasSomeClass;
},
},
}); |
Beta Was this translation helpful? Give feedback.
-
I presume you need to use the rest api and add the |
Beta Was this translation helpful? Give feedback.
-
Forgive me, as I'm not a fully seasoned React developer, but I know enough to figure my way around it. I say this because my terminology might be out of context, which is why I'm seeking clarity.
So, my current rudimentary understanding is that the Interactivity API is a DOM layer for HTML that we would traditionally use vanilla JavaScript for - adding data binding, appending classes, showing/hiding, and general user interactivity with HTML elements. Although it might be used to assist with things like fetch, AJAX, or external API calls, it's not directly meant for that. Its purpose is literally in-browser HTML interactivity and local WP data. Is my understanding correct?
Also, if I were looking to use the core example of using AJAX, which is currently documented in the handbook using jQuery, would I simply add my logic to the view.js file when posting to my action, although I'm not sure how that would work given the dependencies required for using jQuery? Or would I still enqueue and localize scripts separately, then use vanilla JavaScript to add event listeners, in combination of using the Interactive API etc.?
I hope I'm making sense here, but do feel free to ask if I need to explain with more clarity.
Beta Was this translation helpful? Give feedback.
All reactions