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

clean-react-props

v0.4.0

Published

Utility function for passing clean props to DOM elements in React.

Downloads

16,732

Readme

🛁 clean-react-props

npm NPM npm Coveralls github CircleCI Snyk Vulnerabilities for GitHub Repo

Utility functions for passing clean React props to HTML and SVG elements.

Since React 15.2.x, warnings are thrown when attributes are applied to HTMLElements that are not natively supported by React. This utility helps you to prevent those warnings and help you create consistent and clean components. You can read more about it, here.

Install

Via npm:

npm install --save clean-react-props

Via Yarn:

yarn add clean-react-props

How to use

When building components, it’s always a good idea know what properties your component expects and confirm that they handle them properly. But, in some cases your component could end up getting more than it asked for, that's where clean-react-props come in handy.

In order to cleanly compose your components, it's best if they keep the props chain alive (where necessary), and remove the props that are either specific to your component or in addition to what your component expects.

HTMLElements

import cleanProps from 'clean-react-props';

...

  render() {
    const {
      aProp,
      anotherProp,
      children,
    } = this.props;

    return (
      <div {...cleanProps(this.props)}>
        <ChildComponent aProp={aProp} />
        <OtherChildComponent anotherProp={anotherProp} />
        {children}
      </div>
    );
  }

...

SVGElements

React is also opinionated about the attributes that it supports for SVGElements. So, I’ve included a utility that you can use specifically for <svg> tags in your code.

import { cleanSVGProps } from 'clean-react-props';

...

  render() {
    return (
      <svg {...cleanSVGProps(this.props)} xmlns="http://www.w3.org/2000/svg" viewBox="0 0 300 300">
        <circle cx="150" cy="150" r="150" style="fill:#ffda00;" />
        <ellipse cx="95.7" cy="93.5" rx="10" ry="27.5" />
        <ellipse cx="195.7" cy="93.5" rx="10" ry="27.5" />
        <path d="M265 145.5c0 63.5-51.5 115-115 115S35 209 35 145.5" style="fill:none;stroke:#000;stroke-width:6;stroke-miterlimit:10;" />
      </svg>
    );
  }

...

Excluding props

In some cases, it’s handy to be able to exlude valid props from being applied to an element, in the event that prop is maybe used on a child element, or if for some reason that component manages that prop differently. If you run into a spot where you ned to exclude some props, pass an array of the prop names you’d like to exclude.

import cleanProps from 'clean-react-props';

...

  render() {
    const {
      aProp,
      anotherProp,
      children,
    } = this.props;

    return (
      <div {...cleanProps(this.props, ['className'])}>
        <ChildComponent aProp={aProp} />
        <OtherChildComponent anotherProp={anotherProp} />
        {children}
      </div>
    );
  }

...

Specifying custom attributes

React–as of 16.*–now supports custom attributes on components. Which means that as of that release, this package may not as useful as it once was, but it allows you to be specific about the attributes that are applied to the rendered DOM elements. To specify custom attributes that you’d like to retain, just do the following.

import cleanProps from `clean-react-props`;

...

  render() {
    const {
      aProp,
      myCustomAttribute,
      children,
    } = this.props;

    return (
      <div {...cleanProps(this.props, [], ['myCustomAttribute'])}>
        <ChildComponent aProp={aProp} />
        {children}
      </div>
    );
  }

...

License

MIT © Ryan Hefner