Support for React-like `ref`s?
See original GitHub issueWouldn’t it be a nice and useful feature if superfine
supported ref
pseudo properties like React, Preact, Dyo, uhtml etc. do?
const elemRef = { current: null }
// [...]
const content = h('some-element', { ref: elemRef })
Here’s a little demo (based on a R&D web-component toy library that uses a ref
-supporting patched version of superfine
internally):
TypeScript version
JavaScript version
FYI: Here’s a naive quick’n’dirty implementation which obviously only provides part of the requested functionality: LINK
Issue Analytics
- State:
- Created 2 years ago
- Reactions:2
- Comments:23 (14 by maintainers)
Top Results From Across the Web
Refs and the DOM - React
Refs provide a way to access DOM nodes or React elements created in the render method. In the typical React dataflow, props are...
Read more >A complete guide to React refs - LogRocket Blog
Learn how to use React refs, and why it's important to use them only when React can't handle a function call through its...
Read more >Refs to Components | React
React supports a special attribute that you can attach to any component. The ref attribute can be a callback function, and this callback...
Read more >A Beginner's Guide to Dealing With Refs in React - Medium
You pass in a string to ref in the JSX and the ref is saved in this.refs under that string like this: class...
Read more >Everything You Need to Know About Refs in React
Short for “reference”, refs are a way to access underlying DOM elements in a React component. There are many reasons why you would...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Let’s assume you have a fancy component library which is based on
superfine
. Now let’s assume you want to implement a componentVideoPlayer
. ThisVideoPlayer
shall have the usual control buttons video players normally have, likePlay
,Pause
etc. Somewhere in your VDOM tree you have to use ah('video', ...)
virtual node. But how do the event handlers for thosePlay
andPause
buttons work? They need the actualHTMLVideoElement
instance to call theplay()
andpause()
methods on it. But how do you get thisHTMLVideoElement
instance? One way would be to use DOM’squerySelector()
method, but then you would normally need theHTMLElement
of theVideoPlayer
instance itself. That’s of course possible, but using React-like ref objects and ref callbacks is a much nicer, cleaner and more flexible way to do so.If I understand that correctly it may have been possible in the old days of
superfine
to write a functionref
which could be used as follows:h('video', { ...ref(videoRef), src: videoSrc })
or (with JSX)<video {...ref(videoRef)} src={videoSrc}/>
with
This may work the same way as React-like refs do. Nevertheless the
<video ref={videoRef} />
syntax is nicer and those lifecycle properties have been removed insuperfine
for a reason, I guess.Sure, I can provide a PR if you like. I guess the changes in superfine will look something like the following (please ignore the comments on top of the file): https://github.com/js-works/js-element/commit/a7767704a3d1ac1ba5a09d707a55258fc9fbb973
This is working for my small demo, but I have to check some edge cases before I can prepare the PR for superfine. I do not know the exact coding guidelines for the
superfine
project but I guess for themain/index.js
file the credo is “shorter is better” (both in LOC and built distribution package) - please let me know if I am wrong.