plasmic
plasmic copied to clipboard
Event listeners with REST API
I am trying to add an onclick listener to button on the page. Also, I am using REST API to fetch html from Plasmic. In componentProps query documentation there is a line:
This should be the props to be passed to the component, with this you can customize which variants are going to be active, define the value of expected props and override slots content (with text only).
And indeed, when I try to pass function as a prop to api, it fails with code 500. Does that mean that it is not possible to use event listeners with plasmic rest api?
That's right, there's no way to specify event listeners via the REST API. It's unclear what this would mean. For something like React, you can do this, since you are working within a JavaScript runtime, can pass enclosures that can actually interact with other parts of your application, etc. What are you looking for here?
That's unfortunate. I am trying to combine Plasmic and native Web Components (for easier implementation I use Lit). So I've created a simple component, which fetches html from Plasmic via REST and renders it. It works fine, but I guess if adding event listeners is not possible I'll have to do it theother way. Basicly, I want to use full functionality of Plasmic without any framework like React. Is this possible?
Update here. Eventually I've implemented simple querySelector("button[class*=nameOfComponentInPlasmic]") call after html from plasmic is loaded. Works ok so far, but it defenetly would be nicer to have some easier and less error-prone approach to adding event listeners with REST.
Closed by accident :(
Hi @andrewlevada! 👋
Have you try calling with the hydration option?
According to the doc, it looks like what you would like to achieve:
"Since event listeners aren’t going to be attached when only the API result is used, you may have to perform hydration, this way your pages will become responsive to user interaction. You can indicate it with the following query params."
@arenzel thanks for the response. Unfortunately, I couldn't find a way to make hydration of specifically event listeners work with rest api. @yang also pointed out that this is in fact intended.
I don't think that behavior I want to achieve is possible, unless there is a way of manually calling hydration function on client after getting code with rest.