Skip to content

Instantly share code, notes, and snippets.

@statianzo
Last active August 30, 2016 13:47
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save statianzo/91234d26094ad89c1ff742f41b25d737 to your computer and use it in GitHub Desktop.
Save statianzo/91234d26094ad89c1ff742f41b25d737 to your computer and use it in GitHub Desktop.
unknown props warning
const ContactBadge = (props) => (
<div className='Badge' {...props}>
<div>{props.user.name}</div>
<div>{props.contactMethod.type}</div>
</div>
);
export default connect((state, {userId, contactMethodId}) => ({
user: selectUser(state, userId),
contactMethod: selectContactMethod(state, contactMethodId)
}))(ContactBadge);
//Using clone+delete to satisfy unknown props warning
const ContactBadge = (props) => {
const divProps = {...props};
delete divProps.user;
delete divProps.contactMethod;
delete divProps.userId;
delete divProps.contactMethodId;
delete divProps.dispatch;
return (
<div className='Badge' {...divProps}>
<div>{props.user.name}</div>
<div>{props.contactMethod.type}</div>
</div>
);
};
export default connect((state, {userId, contactMethodId}) => ({
user: selectUser(state, userId),
contactMethod: selectContactMethod(state, contactMethodId)
}))(ContactBadge);
//Using destructuring to satisfy unknown props warning
const ContactBadge = ({user, contactMethod, userId, contactMethodId, dispatch, ...rest}) => (
<div className='Badge' {...rest}>
<div>{user.name}</div>
<div>{contactMethod.type}</div>
</div>
);
export default connect((state, {userId, contactMethodId}) => ({
user: selectUser(state, userId),
contactMethod: selectContactMethod(state, contactMethodId)
}))(ContactBadge);
@burakcan
Copy link

burakcan commented Jul 31, 2016

This is our solution in an app which is almost impossible to refactor every component:

// DOMElement.js
import React, { Component } from 'react';
import DOMProperty from 'react/lib/DOMProperty';
import EventPluginRegistry from 'react/lib/EventPluginRegistry';
import pick from 'lodash/pick';
import keys from 'lodash/keys';
import concat from 'lodash/concat';

const reactKeys = [
  'children',
  'dangerouslySetInnerHTML',
  'autoFocus',
  'defaultValue',
  'valueLink',
  'defaultChecked',
  'checkedLink',
  'innerHTML',
  'suppressContentEditableWarning',
  'onFocusIn',
  'onFocusOut',
  'onFocus',
];

const DOMKeys = keys(DOMProperty.properties);
const eventKeys = keys(EventPluginRegistry.registrationNameModules);

const pickProps = pick(
  concat(
    reactKeys,
    concat(DOMKeys, eventKeys)
  )
);

export default function domElement(Komponent) {
  /* eslint-disable react/prefer-stateless-function */
  return class DOMElement extends Component {
    render() {
      return <Komponent { ...pickProps(this.props) } />;
    }
  };
}
// Div.js
import domElement from './DOMElement';
export default domElement('div');
// Usage:
import Div from 'Div'
...
<Div foo="bar" baz /> // No errors

@gaearon
Copy link

gaearon commented Jul 31, 2016

@burakcan This is not a solution we recommend. Those modules are internal and may be changed/removed in any release.

@burakcan
Copy link

burakcan commented Aug 1, 2016

@gaearon, yes we're aware of that. But in that case, there's only one file to change.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment