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

react-callforward

v1.0.0

Published

Like a callback, but forward :)

Downloads

40

Readme

npm version license

react-callforward

We all know about callbacks in react, they provide a way for a parent component to provide the implementation of a function that is triggered by a nested component. What happens when you need the opposite? How do you trigger a command that is implemented in a nested component?

Problem

For example, lets say that you have the following app that displays a video:

+----------------------------------------------------+
|Root                                                |
|                                                    |
| +------------+ +---------------------------------+ |
| |Sidebar     | | Content                         | |
| |            | |                                 | |
| |            | |  +---------------------------+  | |
| |  +------+  | |  |                           |  | |
| |  |play  |  | |  |       video player        |  | |
| |  |button|  | |  |                           |  | |
| |  +------+  | |  |                           |  | |
| |            | |  +---------------------------+  | |
| |            | |                                 | |
| +------------+ +---------------------------------+ |
+----------------------------------------------------+

The Sidebar and the Content components are independent, they are oblivious of the existence of each other. The sidebar has a "play" button that needs to trigger the video.play() method that exists within the scope of the Content component. How would you solve that?

  • alternative #1, using state: The Root component has an isPlaying flag in the state, listens to the click callback of the play button and then propagates the state down to the nested Content component using props. The Content component would compare changes in the props and call the play() method accordingly. It works, but you loose the "imperative" nature of just calling a function; and you'll trigger an, otherwise unnecessary, render of the Root component.
  • alternative #2, using refs: The Content component bubbles up a ref of the video player onto the Root component. The Root component creates an onClick handler that triggers the play() inside the ref and then it passes the handler into the onClick callback of the Sidebar component. It also works, but bubbling things up goes against the "composite" nature of our react components.

If you're happy with either of the above solutions, close this README and stop procrastinating, your app won't write itself.

Solution

The basic idea of a callforward is to divide a method call into two parts: the trigger and the placeholder. The trigger is just a proxy of the actual method call. The placeholder is an empty wrapper that needs to be "implemented" by some other child component.

Take the above video app example, this is how you would solve the problem using a callforward:

function Root() {
    const [onPlay, doPlay] = useCallForward();

    return (
        <div>
            <Sidebar onClick={onPlay} />
            <Content doPlay={doPlay} />
        </div>
    )
}

function Sidebar({ onClick }) {
    return <button onClick={onClick}>play</button>
}

function Content({ doPlay }) {
    const videoEl = useRef();

    useCallHolder(() => {
        videoEl.current.play();
    }, doPlay);

    return <video ref={videoEl} />
}

The above example has been simplified for brevity. To see a running example, check the following codesandbox:

Edit call-forward-example

Potential

I imagine several use cases where components could be stripped away of opinionated control UI (buttons, inputs, etc), but still provide the "logic" to execute such actions:

  • a simple "video" component providing play, pause, scrubbing methods
  • any type of "data list" component providing a "refresh" method
  • dialogs and popup components providing an imperative "open" / "close" method (thus, hiding the open/close state within the component itself)
  • long text components providing "scrolling" (eg: "go to top") methods

Installation

to install the library, do as usual:

npm install --save react-callforward