layershifter / babel-plugin-transform-react-handled-props

Generates handledProps from defaultProps and propTypes during the build :sparkles:
MIT License
19 stars 4 forks source link

Doesn't work with React wrapper methods #62

Closed kalbert312 closed 5 years ago

kalbert312 commented 5 years ago

Wrapping a function component with React.forwardRef or React.memo causes it to get ignored.

Example:

const Button = React.forwardRef((props, ref) => {
    const rest = // get rest of props not handled
    return (<button ref={ref} {...rest}>{props.label}</button>);
});

Button.propTypes = {
    label: PropTypes.string,
};

export default Button;

Can also be chained, e.g React.memo(React.forwardRef(() => ...))

kalbert312 commented 5 years ago

I got around this with

const Button = (props, ref) => {
//...
}

const forwarding = React.forwardRef(Button);

Button.propTypes = {
    //...
};

export default forwarding;

forwardRef/memo don't support propTypes since a new object with a render method is returned.

layershifter commented 5 years ago

Great catch 👍 It's a priority stuff for me, will work on a fix.

layershifter commented 5 years ago

Published babel-plugin-transform-react-handled-props@1.1.0, please check and provide feedback 🐱

kalbert312 commented 5 years ago

It works! Thanks.