Clean React Props
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
-
Warning: Received `myCustomProp` as a prop to a DOM element. If you intended to pass this as a custom attribute, etc.
cause This is the core React warning that `clean-react-props` is designed to prevent. It occurs when a non-standard HTML attribute or a component-specific prop is passed directly to a native DOM element (e.g., a `div` or `span`) without being filtered.fixWrap your DOM element's props with `cleanProps(this.props)` or `cleanSVGProps(this.props)` to automatically filter out unknown props. If `myCustomProp` is intended for a child component, ensure it's removed from the current component's props before spreading to the DOM element. -
TypeError: cleanProps is not a function
cause This typically occurs if you attempt to import the default export `cleanProps` as a named export, or vice-versa for `cleanSVGProps` in an ESM context, or if using CommonJS `require` incorrectly.fixFor `cleanProps` (default export), use `import cleanProps from 'clean-react-props';` or `const cleanProps = require('clean-react-props');`. For `cleanSVGProps` (named export), use `import { cleanSVGProps } from 'clean-react-props';` or `const { cleanSVGProps } = require('clean-react-props');`. -
Warning: Unknown event handler property `onMyCustomEvent`. Did you mean `onMycustomevent`?
cause `clean-react-props` filters based on a list of known valid HTML/SVG attributes and event handlers. If you define a custom event handler that isn't on React's (and thus the library's) whitelist, it will be stripped, and React might still warn if the original prop object contains it.fixEnsure your custom event handlers are not passed directly to a native DOM element if `clean-react-props` is being used, or ensure they conform to standard React event naming conventions. If you need to pass a truly custom event handler to a custom component, extract it before applying `cleanProps` to a native DOM element.
Warnings
- gotcha React 16 and later versions introduced native support for custom attributes on HTML/SVG elements. This diminishes the primary use case of `clean-react-props` for developers who wish to pass custom `data-*` or other non-standard attributes directly to the DOM without warnings. The library remains useful for strict enforcement of standard attributes and explicit prop filtering.
- deprecated The package `braces` (a dependency of an indirect dependency) had a potential vulnerability (CVE-2018-1000006). This was addressed by `clean-react-props` updating its dependencies and specifically adding `braces@>=2.3.1` in `v0.3.2`.
- gotcha When using `cleanProps` or `cleanSVGProps`, any props intended for child components or that have special handling within the current component (e.g., `className` for a styled component wrapper) must be explicitly excluded via the second argument. If not excluded, they will be filtered out.
Install
-
npm install clean-react-props -
yarn add clean-react-props -
pnpm add clean-react-props
Imports
- cleanProps
import { cleanProps } from 'clean-react-props';import cleanProps from 'clean-react-props';
- cleanSVGProps
import cleanSVGProps from 'clean-react-props';
import { cleanSVGProps } from 'clean-react-props'; - All functions (CommonJS)
import cleanProps from 'clean-react-props'; // CommonJS environments const cleanProps = require('clean-react-props').cleanProps;const cleanProps = require('clean-react-props'); const { cleanSVGProps } = require('clean-react-props');
Quickstart
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 />);