Finally, the renderer on the host side converts the payload into UI. Internally, the renderer takes care of converting each node in the component tree by recursively calling React CreateElement with the implementation and the props for each child. Now that we've covered how UI is rendered, I'll walk you through how UI gets updated based on user inputs.
Here, we have an example of an external script rendering a text field with the value prop managed internally using state. When a user types into the text field, the onChange proxy is called, which triggers the setTextValue method to update the state inside the worker. The text field value prop is updated with a new text value. Internally, React Reconciler calls the remote route to handle updating their component tree. This in turn sends an updated message through the receiver on the host, which then triggers an update to its internal route. Finally, the renderer is called with the updated route that contains the updated prop for the text field, resulting in a UI being rendered.
Now that we've covered how Argo is built on web using React, I'll talk about how React can be swapped out. Looking back at the end to end flow for rendering UI, a few pieces can be replaced. Different client libraries can be used by the script to construct the UI. For example, the Argo React library can be replaced with vanilla JS or Vue.js. Similarly, we can swap out the renderer on the host side with another implementation. React DOM can be replaced with Swift or Kotlin or React Native. Swapping out the pieces is possible as long as the contract between the JavaScript sandbox and the host is maintained. The same render payload with the serialized component tree is sent no matter which library the external script is using to construct the UI. Similarly, no matter how the UI is rendered on the host, when a user interaction on the UI results in calling a handler on the external script, the host triggers the handler via a proxy.
Now, I'll show a quick pre-recorded demo of how all the pieces work together. Here, I have an external script rendering a form and a banner into Shopify on web and iOS. For this example, I've enabled live reload, which will allow us to see the updated UI as we change our code. Now, I'll demonstrate how we can pull in and utilize additional APIs provided by Shopify. The library provides useful hooks in order to do that. We'll call the use extension API hook. And from the API, we'll pull out the Toast object, which contains a method to call to show a Toast message. Now, all we want to do is output the content of our form in a Toast message as a JSON string. Once this refresh, we can test out our new callback. So I'm filling out the form. And now if I hit submit, I get a Toast message with my form data. I can also do the same on iOS.
Comments