Open IanVS opened 8 years ago
I second this one, plenty boilerplate if you import a library. OTOH, it might be better to abstract libraries away in this manner? But would be nice to have a choice.
I'd be happy to review a PR that adds this as an option.
Some of this, I think, could be covered reasonably straightforwardly by checking the prefix of the handler name. Only apply the rule if it begins with (this.
but not this.props
) or something other than props
.
But I don't think that's a good approach. It's unchecked by default, can be bypassed quite easily, and would remove all enforcement when using destructured props. Anything more advanced would need to know about more than just the JSXAttribute
. Any pointers on other rules that could inspire an approach?
(First time here looking at rule implementation)
I think this option could solve my problem as well. I have a situation where I want to avoid the rule to check method from prop
This below is valid because onStop
comes from property
// GOOD
interface CarProps {
brand: string;
onStop: () => void;
}
const Car = ({ brand, onStop }: CarProps) => {
return (
<h1>{brand}</h1>
<button onClick={onStop} />Stop</button>
)
}
However, the rule will fail if I have this component method
// BAD
interface CarProps {
brand: string;
}
const Car = ({ brand }: CarProps) => {
// It should be `handleStop`
const onStop = () => { console.log('stop'); }
return (
<h1>{brand}</h1>
<button onClick={onStop} />Stop</button>
)
}
Why not ({ brand, onStop: handleClick }): CarProps) => {
?
This is a useful rule, but it can sometimes get annoying when I want to use a prop or a method from a library directly as a handler. I think the real benefit to the rule is to force me to name my component's methods appropriately, but as it currently works, I sometimes need to do silly things like
const handleSelect = props.entities.toggleItem;
.I would really like an option which prevents:
but still allows:
Does that make any sense?