react-callforward v1.0.0
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
Rootcomponent has anisPlayingflag in the state, listens to the click callback of the play button and then propagates the state down to the nestedContentcomponent using props. TheContentcomponent would compare changes in the props and call theplay()method accordingly. It works, but you loose the "imperative" nature of just calling a function; and you'll trigger an, otherwise unnecessary, render of theRootcomponent. - alternative #2, using refs: The
Contentcomponent bubbles up a ref of the video player onto theRootcomponent. TheRootcomponent creates anonClickhandler that triggers theplay()inside the ref and then it passes the handler into theonClickcallback of theSidebarcomponent. 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:
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