npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@smart-hooks/use-smart-ref

v4.1.2

Published

Extends useRef logic

Downloads

2

Readme

useSmartRef

A ref attribute of a DOM element inside a React component gives a familiar way to get aware of when the element is being created and provide control over it. The attribute may receive a special object (RefObject) as a value whose purpose is to keep the node reference in its mutable current property starting from the moment the DOM element has been created. Besides, the ref attribute may also contain callback ref, which is a callback that runs when the DOM element has become a part of the structure of the component. The standard hook useRef provides creating such a holder object that survives after every render of the component, so its current member can carry a reference to the element consistently while it is staying mounted in the DOM tree. Alongside using callback ref coupled with other hooks, useRef allows managing many use cases regarding DOM elements inside the component. But the hook itself does not provide the common universal approach to that managing.

How can we have control over phases of creating a new DOM element inside a component? We can view them as events or specific life cycles (

  1. The DOM element has been created and attached to the inner DOM structure of the component.
  2. The element has been removed from it).

It would help a lot if we could attach a callback to the event when the element is creating inside the component and a clean-up callback having a chance to fire up when the component gets rid of the element, or the entire component loses its existence.

That is where useSmartRef comes in.

Install

npm install @smart-hooks/use-smart-ref --save

Usage

import React, { useRef } from 'react';
import { useSmartRef } from '@smart-hooks/use-smart-ref';

function Component() {
  refRecord = useRef();

  callbackRef = useSmartRef((el) => {
    // The code will get invoked at the moment when the DOM element gets ready

    return () => {
      // Clean-up routine
    };
  }, refRecord);

  return <div ref={callbackRef}>{/* ... */}</div>;
}

callbackRef is a callback ref passed to an attribute ref of a target DOM element. It gets received from the hook useSmartRef. The first argument of the hook is a function that runs at the time when the DOM element has been created and attached to the parent component. It optionally returns another function that has a clean-up role and runs once the element has got removed or the whole component has been unmounted. It is pretty the same scheme that we are familiar with using the standard hook useEffect. The second argument in useSmartRef is optional. It is a RefObject created by the hook useRef. So it will keep a reference to the corresponding DOM element in its current property as if we directly pass it to the ref attribute of the element.

Digging Deep

In the case of using a state or props of a component as a selector determining what jsx element to activate, it is possible to attach the same emitted callback ref to each alternative. Whenever an element has been substituted with another one, a clean-up procedure runs first, and the callback gets invoked, having the reference to the newly activated element as an argument, memoizing a new clean-up callback prepared for the next switch or complete component removal.

function TestUseSmartRef({ choice }) {
  callbackRef = useSmartRef((el) => {
    // ...
  });

  return choice === 0 ? (
    <div ref={callbackRef}>{/* ... */}</div>
  ) : (
    <img ref={callbackRef} src={imageSource}>
      {/* ... */}
    </img>
  );
}

The callback passed to useSmartRef is being memoized on every state or props change with new values of variables from the outer scope. And meanwhile, the returned callback ref itself is not changing in the course of the life of the component.