I am trying to use forwardRef for a Button in a project using eslint and prop-types.
This is what I tried so far, and the errors I get each time:
First attempt
function Button ({ action = { callback: () => {}, title: 'unknown' } }, ref) {
return (<button ref={ref} onClick={action.callback} title={action.description} type="button">{action.icon || action.title}</button>)
}
Button.propTypes = {
action: Action.isRequired
}
export default forwardRef(Button)
This will give me the following warning in the console: Warning: forwardRef render functions do not support propTypes or defaultProps. Did you accidentally pass a React component?
Second attempt
function ButtonFunction ({ action = { callback: () => {}, title: 'unknown' } }, ref) {
return (<button ref={ref} onClick={action.callback} title={action.description} type="button">{action.icon || action.title}</button>)
}
const Button = forwardRef(ButtonFunction);
Button.propTypes = {
action: Action.isRequired
}
export default ButtonFunction;
I get: action is missing in props validation
.
Third attempt
const Button = forwardRef(({ action = { callback: () => {}, title: 'unknown' } }, ref) => {
return (<button ref={ref} onClick={action.callback} title={action.description} type="button">{action.icon || action.title}</button>)
});
Button.propTypes = {
action: Action.isRequired
}
export default Button;
This time, I get: Component definition is missing display name
.
So what is the correct way to do this?