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 🙏

© 2025 – Pkg Stats / Ryan Hefner

as-with

v0.1.0

Published

as-with composition pattern

Downloads

4

Readme

as-with

Build Status Coverage Status Release Version Npm Downloads Greenkeeper badge

A re-usable pattern for rendering a React component through another React component.

Why

It can be tedious writing higher order components (HOC) to combine functionality of simple components. For example, a UI Framework such as rbx – which provided the initial implementation of this code – largely transforms components' props into a className prop which is then used to style a DOM element.

import { Button } from "rbx";

// renders: <button className="button is-primary">The button</button>
const myButton = <Button color="primary">The button</Button>;

// renders: <a className="button is-primary">The link</a>
const myLink = (
  <Button as="a" color="primary">
    The Link
  </Button>
);

Of course, simple components (like the naïve <Button> implementation above) should also forward refs – those handy React objects that let you manipulate components or the underlying JSX elements.

There are a handful of constraints in this problem-space, that enable sane usage:

  1. a component should be able to render as any other component (for example, as={MyComponent})
  2. the receiving component must accept whatever props the providing component passes (for example, MyComponent should accept className: string if used as the as prop for <Button>)
  3. all unconsumed props should be forwarded to the underlying as component.
  4. if one of the required props for the component provided to as conflicts with an optional or required prop of the base component (for example, <Button> takes an optional prop color and perhaps <MyComponent> takes a required prop color), then the base-component (e.g. <Button>) should accept an additional prop with.
  5. all props in with should be provided to the underlying component, and should take precedence over the extra props supplied to the base component.
  6. with props can be indefinitely nested and provide their own as prop and with prop (assuming the as types also comply with the as-with interface specification) .
  7. the ref prop for the base component (e.g. <Button>) should reflect the utlimate (or tail type) component that is finally rendered.

How

By wrapping React's provided forwardRef and it's related TypeScript types, we provide a function and the related interfaces for supporting this pattern – in a strictly typed way.

What

The forwardRefAs function provides the functionality for this composition pattern and has roughly the interface specified:

function forwardRefAs<TDefaultComponent, TOwnProps, TForwardsProps> (
  constructor: (props: TOwnProps, ref: any) => React.ReactElement<any> | null;
  defaultProps: {
    as: TDefaultComnponent,
  };
) => React.ComponentType<TOwnProps & React.ComponentProps<TAsComponent> & React.RefAttributes<TAsComponent> ...);