I’ve created a notebook that provides some helpful tools to to create and render React components with Observable.
Here’s what it does:
There’s a jsx function that uses the htm library to allow you to use JSX-like syntax directly in Observable.
You can call render to render some JSX to the page
You can call component to create a component that maintains its identity even if values used by the component change. This is accomplished by storing the returned component object in a cache keyed by the original component’s source code.
I’ve also created wrappers for memo and forwardRef that use the same implementation
Finally, virtually all the React exports have been re-exported for easy use:
import { jsx, component, render, useState } from '@j-f1/react'
(You can still import the original React and ReactDOM objects if you like)
What do you think? Are there any fatal flaws with this method?
I found the notebook days ago, but just found this forum thread, so I was missing a bit of context. Also, I was missing some essential bits of knowledge, so I had a little trouble understanding everything until I educated myself. Could you add some more explanatory prose to the notebook?
I was able to port one of my React Codepens using your code. My notebook may serve as a “broad and shallow” example for someone (perhaps too broad).
Thanks for the feedback @vorth! I added a little more explanation around htm syntax, created a super basic “Hello world” example, reworded the docs a little bit, and linked to your example notebook.
Feel free to send suggestions in the notebook if you have any specific ideas for changes!