Clean React Props

0.4.0 · maintenance · verified Sun Apr 19

clean-react-props is a utility package for React applications, currently at version 0.4.0, designed to filter out unknown or unwanted properties before they are passed to standard HTML or SVG DOM elements. Its primary purpose is to prevent React 15.2.x warnings (e.g., 'Warning: Unknown prop `myProp` on tag `div`') that arise when non-standard or component-specific props are inadvertently rendered onto native DOM elements. The library provides `cleanProps` for HTML and `cleanSVGProps` for SVG elements, allowing developers to ensure only valid attributes and event handlers are applied. While React 16 and later versions introduced support for custom attributes on DOM elements, mitigating some of the original necessity, `clean-react-props` remains useful for enforcing strict adherence to standard HTML/SVG attributes and for explicitly excluding props that should not be passed down. Releases are infrequent, primarily focusing on dependency updates and adding support for new standard attributes/events.

Common errors

Warnings

Install

Imports

Quickstart

Demonstrates basic usage of `cleanProps` to filter props passed to a DOM element, including how to explicitly exclude certain valid props.

import cleanProps from 'clean-react-props';
import React from 'react';

// Example Component to demonstrate cleanProps usage
function MyComponent({ aProp, anotherProp, className, children, onClick, ...rest }) {
  // `aProp` and `anotherProp` are for internal component logic
  // `className` is explicitly excluded from the div to be handled by a child or styled component
  // `onClick` is a standard event handler, which cleanProps will retain
  // `rest` might contain unknown/custom props that cleanProps will filter

  return (
    <div {...cleanProps({ ...rest, aProp, anotherProp, className, onClick }, ['className'])}>
      <p>This div has clean props.</p>
      <ChildComponent aProp={aProp} />
      {children}
      <button onClick={onClick}>Click Me</button>
    </div>
  );
}

// Example usage in an app context
function App() {
  const handleButtonClick = () => console.log('Button clicked!');
  return (
    <MyComponent
      aProp="internal-value"
      anotherProp="another-internal"
      data-test-id="my-component-test" // Valid custom attribute in React 16+
      customAttribute="should-be-filtered" // Likely filtered by cleanProps
      onClick={handleButtonClick}
      style={{ border: '1px solid blue' }}
    >
      <span>Hello, world!</span>
    </MyComponent>
  );
}

// In a real application, you would render this using ReactDOM.render
// For demonstration, we'll log its representation.
console.log('App component configured with clean-react-props:', <App />);

view raw JSON →